Forum Discussion
Difference between two Measures with Cumulative Totals
Hi,
I am currently working on calculating the difference in cumulative totals between 2 user selected dates.
I have created some sample data (using Enter Data in Power BI) for testing purposes.
The Difference measure created is not showing the difference between the cumulative values.
The published link is as below:
Please help. Also, any suggestions or optimal ways on calculating the cumulative totals would be appreciated. Thanks.
Regards,
Vishy
hi, Anonymous
As I said above, you use Edit interactions Function to keep the two columns in the same table to filter different measure.
You couldn't do that.
And here are two ways for you as a reference:
way1:
Duplicate the basic table, do Cumulative Sum 1 Written Premium and Cumulative Sum 2 Written Premium in different table.
Step1:
Add a new table
Sheet2 = Sheet1
Step2:
Add a Year fact table
Year = VALUES(Sheet1[PROGRAM_YR] )
Step3:
Create the relationship between them like this:
Step4:
Create the total measure in different table
Cumulative Sum 1 Written Premium = CALCULATE( SUM(Sheet1[WRITTEN_PREMIUM_AT]), FILTER(ALLSELECTED(Sheet1[CORP_PERIOD_DT 1]),Sheet1[CORP_PERIOD_DT 1]<=MAX(Sheet1[CORP_PERIOD_DT 1])))
Cumulative Sum 3 Written Premium = CALCULATE( SUM(Sheet2[WRITTEN_PREMIUM_AT]), FILTER(ALLSELECTED(Sheet2[CORP_PERIOD_DT 1]),Sheet2[CORP_PERIOD_DT 1]<=MAX(Sheet2[CORP_PERIOD_DT 1])))Difference Written Premium measure = [Cumulative Sum 1 Written Premium]-[Cumulative Sum 3 Written Premium]
By the way, Difference Written Premium should be a measure instead of column.
Step5:
Drag the CORP_PERIOD_DT 1 from different table(Sheet1 and Sheet2 ) to filter differnt measure. Do not use Edit interactions Function.
here is way1 pbix file, please try it.
Best Regards,
Lin
hi, Anonymous
Way2:
Here is a similar post for you refer to:
https://www.sqlbi.com/articles/filtering-and-comparing-different-time-periods-with-power-bi/
And for your case, you could do it like this:
Step1:
Add a date table and create the relationship with Sheet1 by CORP_PERIOD_DT 1
Step2:
Add a previous date table and create the relationship with date table, make this relationship unactive
Previous Date = ALLNOBLANKROW ( 'Date' )
Step3:
Create the measure like below:
Cumulative Sum 3 Written Premium = CALCULATE ( [Cumulative Sum 1 Written Premium], ALL ( 'Date' ), USERELATIONSHIP ( 'Date'[Date], 'Previous Date'[Date] ) )Difference Written Premium measure = [Cumulative Sum 1 Written Premium]-[Cumulative Sum 3 Written Premium]
Step4:
Drag the year month column from date table and Previous Date table as slicer to filter the measure.
and here is way2 pbix file, please try it.
Best Regards,
Lin
14 Replies
- Greg_Deckler
Community Champion
I can't see the calculations behind your measures. Can you post the PBIX or a link to it?
In general, kind of best practice is to use the Running Total quick measure built into Power BI. I also have another method of doing it here:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TITHW/m-p/434008- AnonymousNot applicable
Hi Greg,
How do I attach the pbix file? I do not see the Attachments option when I compose a message.
I am pasting the measures created here for your reference.
Cumulative Sum 1 = CALCULATE(SUM(Table1[Sales]),FILTER(ALLSELECTED(Table1[Date Filter 1]), Table1[Date Filter 1] <= MAX(Table1[Date Filter 1])))Cumulative Sum 2 = CALCULATE(SUM(Table1[Sales]),FILTER(ALLSELECTED(Table1[Date Filter 2]), Table1[Date Filter 2] <= MAX(Table1[Date Filter 2])))Difference = [Cumulative Sum 2] - [Cumulative Sum 1]Do let me know if there is any way I can attach the pbix file and I can get that attached as well.Thanks,Vishy- v-lili6-msft
Community Support
hi, Anonymous
Based on my test on your measures,
Cumulative Sum 1 = CALCULATE(SUM(Table1[Sales]),FILTER(ALLSELECTED(Table1[Date Filter 1]), Table1[Date Filter 1] <= MAX(Table1[Date Filter 1])))Cumulative Sum 2 = CALCULATE(SUM(Table1[Sales]),FILTER(ALLSELECTED(Table1[Date Filter 2]), Table1[Date Filter 2] <= MAX(Table1[Date Filter 2])))I guess that you should use Edit interactions Function to keep the two columns in the same table to filter different measure?
If so, you could do it like this. Here is a similar post for you refer to:
https://www.sqlbi.com/articles/filtering-and-comparing-different-time-periods-with-power-bi/
If not your case, please share your sample pbix file, You can upload it to OneDrive and post the link here. Do mask sensitive data before uploading.
Best Regards,
Lin