How to compare old data to new data and display result
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
I use next.js with prisma for dashboard. On the dashboard there are cards that display like "x amount of items purchased" and "x visitors" etc...
Bellow that I want to display change - for example today there were total of 1000 visitors but 1 month ago there were only 500 so message is "+100% growth from last month"
How do I implement something like this?
Bellow that I want to display change - for example today there were total of 1000 visitors but 1 month ago there were only 500 so message is "+100% growth from last month"
How do I implement something like this?
Answered by B33fb0n3
you can check the stats of each month and then compare them. So get your stats:
one month ago -> 500
current -> 1.000
Then calculate:
((1000-500)/500)*100
And you will get the result "100". Now you can display this value
one month ago -> 500
current -> 1.000
Then calculate:
((1000-500)/500)*100
And you will get the result "100". Now you can display this value
7 Replies
@Rhinelander I use next.js with prisma for dashboard. On the dashboard there are cards that display like "x amount of items purchased" and "x visitors" etc...
Bellow that I want to display change - for example today there were total of 1000 visitors but 1 month ago there were only 500 so message is "+100% growth from last month"
How do I implement something like this?
you can check the stats of each month and then compare them. So get your stats:
one month ago -> 500
current -> 1.000
Then calculate:
((1000-500)/500)*100
And you will get the result "100". Now you can display this value
one month ago -> 500
current -> 1.000
Then calculate:
((1000-500)/500)*100
And you will get the result "100". Now you can display this value
Answer
RhinelanderOP
But i have only latest value in my db. Where do i save old values without changing db schema.
@Rhinelander But i have only latest value in my db. Where do i save old values without changing db schema.
you can either create monthly stats or daily stats or however you would like to collect large amounts of data. Technically you can even count everytime the user visits. However thats ineffective. So you might want to create stats objects for a specific time span and then count them either together or directly use the value
RhinelanderOP
Going to try and mark as solution as i finish. Until then thank you!
Happy to help