Forum Discussion
Calculating daily differences
- 5 years ago
Hi ripstaur ,
First create an index column;
Then create a column as below:
Daily Cases = VAR _maxvalue = CALCULATE ( MAX ( 'Table'[Cases] ), FILTER ( 'Table', 'Table'[County] = EARLIER ( 'Table'[County] ) && 'Table'[Index] < EARLIER ( 'Table'[Index] ) ) ) RETURN 'Table'[Cases] - _maxvalueAnd you will see:
For the related .pbix file,pls see attached.
Best Regards,
KellyDid I answer your question? Mark my reply as a solution!
Thank you , Edhans. This problem is like calculating moving ranges, but I need to be able to do it per county across the date range. I put together a sample (with an extra column showing the desired result of the calculation. When shifting from one county to another, the date is going to drop back to the starting date for the time period. In the result cell for that record, the value should equal the "cases" value for that day.
| State | County | Date | Cases | Desired Result (Daily Cases) | |
| Alabama | Abbot | 1/1/2020 | 1 | 1 | |
| Alabama | Abbot | 1/2/2020 | 1 | 0 | |
| Alabama | Abbot | 1/3/2020 | 2 | 1 | |
| Alabama | Abbot | 1/4/2020 | 2 | 0 | |
| Alabama | Abbot | 1/5/2020 | 3 | 1 | |
| Alabama | Abbot | 1/6/2020 | 3 | 0 | |
| Alabama | Abbot | 1/7/2020 | 3 | 0 | |
| Alabama | Abbot | 1/8/2020 | 4 | 1 | |
| Alabama | Abbot | 1/9/2020 | 4 | 0 | |
| Alabama | Abbot | 1/10/2020 | 5 | 1 | |
| Alabama | Billings | 1/1/2020 | 0 | 0 | |
| Alabama | Billings | 1/2/2020 | 0 | 0 | |
| Alabama | Billings | 1/3/2020 | 2 | 2 | |
| Alabama | Billings | 1/4/2020 | 2 | 0 | |
| Alabama | Billings | 1/5/2020 | 3 | 1 | |
| Alabama | Billings | 1/6/2020 | 4 | 1 | |
| Alabama | Billings | 1/7/2020 | 4 | 0 | |
| Alabama | Billings | 1/8/2020 | 4 | 0 | |
| Alabama | Billings | 1/9/2020 | 5 | 1 | |
| Alabama | Billings | 1/10/2020 | 5 | 0 | |
| Delaware | DeForbes | 1/1/2020 | 0 | 0 | |
| Delaware | DeForbes | 1/2/2020 | 1 | 1 | |
| Delaware | DeForbes | 1/3/2020 | 1 | 0 | |
| Delaware | DeForbes | 1/4/2020 | 1 | 0 | |
| Delaware | DeForbes | 1/5/2020 | 3 | 2 | |
| Delaware | DeForbes | 1/6/2020 | 3 | 0 | |
| Delaware | DeForbes | 1/7/2020 | 5 | 2 | |
| Delaware | DeForbes | 1/8/2020 | 7 | 2 | |
| Delaware | DeForbes | 1/9/2020 | 7 | 0 | |
| Delaware | DeForbes | 1/10/2020 | 7 | 0 |
Hi ripstaur ,
First create an index column;
Then create a column as below:
Daily Cases =
VAR _maxvalue =
CALCULATE (
MAX ( 'Table'[Cases] ),
FILTER (
'Table',
'Table'[County] = EARLIER ( 'Table'[County] )
&& 'Table'[Index] < EARLIER ( 'Table'[Index] )
)
)
RETURN
'Table'[Cases] - _maxvalue
And you will see:
For the related .pbix file,pls see attached.
Best Regards,
Kelly
Did I answer your question? Mark my reply as a solution!
- ripstaur5 years ago
Helper III
Thanks, Kelly! This worked like a charm. I'm still trying to make a PowerQuery solution work, but this DAX solution is absolutely one great solution. My question now becomes - which is a more efficient solution--the DAX modeling or a PowerQuery one, if we can figure it out? I am going to replicate this for the number of deaths per county as well. Since there are about 3400 counties in the US, and the daily report also include extras (e.g., US territory counts, counts of cases that were reported without a county affiliation), I add 3400+ new records every day , with a State column, a county column, a lat and long column, a FIPS column, cumulative cases, cumulative deaths, daily cases and daily deaths. So would it be more efficient to do this in modeling, or querying?
Best regards,
Rip