Forum Discussion
Sum Dollar value between 2 dates
- 8 years ago
Hi, I made a simple example with your data and a calculated table In this file.
Have fun with DAX!
Alberto Ferrari
http://www.sqlbi.com
Hi malcolms,
When you select Area, there is no impact on the chart. That is because when you create a calculated column Date[Dollar], you get all areas dollars. And the calculated column would not be changed by slicer, you can review this knowledage base for more details.
So for your requirement, you need to get different area's dollars. I create the following sample table named 'Test'.
Then create a date table using the formula.
Date = CALENDAR(MIN(Test[Start Date]),MAX(Test[End Date]))
In the Date table, create calculated column using the formulas.
AA-Dollar = CALCULATE(SUM(Test[Dollar]),
FILTER(Test,
Test[Start Date]<='Date'[Date]&&Test[End Date]>='Date'[Date]&&Test[Area]="AA"
)
)
BB-Dollar = CALCULATE(SUM(Test[Dollar]),
FILTER(Test,
Test[Start Date]<='Date'[Date]&&Test[End Date]>='Date'[Date]&&Test[Area]="BB"
)
)
CC-Dollar = CALCULATE(SUM(Test[Dollar]),
FILTER(Test,
Test[Start Date]<='Date'[Date]&&Test[End Date]>='Date'[Date]&&Test[Area]="CC"
)
)
DD-Dollar = CALCULATE(SUM(Test[Dollar]),
FILTER(Test,
Test[Start Date]<='Date'[Date]&&Test[End Date]>='Date'[Date]&&Test[Area]="DD"
)
)
Please note, I think we should use the sum function(in bold) rather than max in the post. For example, the dollar from 2017/4/1 to 2017/4/10 is 10, another record dollar 2017/4/5 to 2017/4/10 is 20, the total dollar should be (10+20) during 2017/4/5-2017/4/10, rather than the max value 20.
Finally, you need to create a measure to get the corresponding total dollar based on the selected area value.
Total-dollar = SWITCH(SELECTEDVALUE(Test[Area]),
"AA",SUM('Date'[AA-Dollar]),
"BB",SUM('Date'[BB-Dollar]),
"CC",SUM('Date'[CC-Dollar]),
"DD",SUM('Date'[DD-Dollar])
)
Create a chart, select the data as x-axis, the measure as value, then you select different in slicer, there is impact on the chart. Please see the screenshot, and you can download the attachment to test.
Select Area "AA"Select Area "CC"
Best Regards,
Angelia
- malcolms9 years agoFrequent VisitorThanks, This approach is good when I have limited number of values in the area field. But in my case there is possibility of the number of areas increasing in the future and I want the calculation to provision for it automatically. Also if I select no value in the slicer the total field is blank. Can we have an approach where no matter how many values or dimension are being added to the fact table it dynamically give me the sum like it gives me in the below calculation I use to count the number of employees between a time period. Count of Active Vacancies = VAR currentDate = MAX ( 'Dates'[Date] ) RETURN CALCULATE ( COUNTROWS ( Vacancy ), FILTER ( Vacancy, ( Vacancy[Vacancy Start]<= currentDate && Vacancy[Filled] >= currentDate ))
- v-huizhn-msft8 years agoMicrosoft Employee
Hi malcolms,
I thought the approch using VAR before, but the current data is dynamic, and it seems to compare the mutiple currents dates to mutiple start/end date. It always returns error message. So that I post the solution above, I will post update if I find another better solution.
Best Regards,
Angelia - AlbertoFerrari8 years agoMost Valuable Professional
Hi Malcolm,
It looks like you are using a calculated column where you could easily use a measure, it is a very common mistake, if you are coming from an Excel background, where everything is a column :)
If your dataset is a small one (few millions rows), then you can author a measure like this one:
Dollars = VAR CurrentStartDate = MIN ( 'Date'[Date] ) VAR CurrentEndDate = MAX ( 'Date'[Date] ) RETURN CALCULATE ( SUM ( Test[Dollar] ), Test[Start Date] <= CurrentStartDate, Test[End Date] >= CurrentEndDate )
Being a measure, it is completely dynamic and will obey any filter you place in the visuals. If, on the other hand, you need to work on larger models, then I suggest you to take a look at this article I have written some time ago: https://www.sqlbi.com/articles/analyzing-events-with-a-duration-in-dax/.
Moreover, I wrote a ton of these common calculations in the time intelligence section of this book, which I guess you might find interesting: https://www.sqlbi.com/books/analyzing-data-with-microsoft-power-bi-and-power-pivot-for-excel/.
Please let me know if this solves your scenario.
Have fun with DAX!
Alberto Ferrari
http://www.sqlbi.com- malcolms8 years agoFrequent Visitor
Hi Alberto,
Thanks for the reply. Sorry for the delay. I had issues posting my reply.
I did use a measure initially and had visited your site for reference. The problem here is that the numbers do not roll up. The second Image shows the daily cost of the employee. So from 3rd May to 23rd May, I was paying $330. From 24th May to 31st May I was paying 763. In total I paid 330*21 & 763*8= 6930 +6104 = 13034. Instead, if you look at the 1st image you would see it displays $330 for May. I'm ok even if the values cumulates on a daily basis. But would like to see the value 9734 for the month of May.
Regards
Malcolm