Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hey guys,
First post in here! I have some trouble with my dax to calculate my subtotals in a cumulative way in a Year-to-date scenario. Basically, it works in the similar Month-to-date scenario when I'm choose single months in my slicer, but not when I'm trying to include multiple months for YTD.
My dax formulas related to the YTD-scenario looks like this:
1)
2)
Slicers that's used in this context: 'Date'[Year], 'Date'[Month]'.
The MTD-scenario (that's working perfectly) looks like this:
1)
Solved! Go to Solution.
@pelusch try with this new version:
no one needs to apologize, we are working together 🙂
BBF
This is the first part of my data:
Year | Month | Account Category Name | Account Category Code | Account Subcategory Name | Account Subcategory Code | Amount |
2024 | 1 | Totala Intäkter | 10 | Försäljning | 100 | 500 |
2024 | 2 | Totala Intäkter | 10 | Försäljning | 100 | 600 |
2024 | 1 | Bruttovinst | 20 | Varukostnad | 200 | -100 |
2024 | 2 | Bruttovinst | 20 | Varukostnad | 200 | -200 |
2024 | 1 | Driftresultat | 30 | Personalkostnad | 300 | -350 |
2024 | 1 | Driftresultat | 30 | Förbrukningskostnader | 310 | -100 |
2024 | 1 | Driftresultat | 30 | Övriga Kostnader | 320 | -50 |
2024 | 2 | Driftresultat | 30 | Personalkostnad | 300 | -400 |
2024 | 2 | Driftresultat | 30 | Förbrukningskostnader | 310 | -150 |
2024 | 2 | Driftresultat | 30 | Övriga Kostnader | 320 | -40 |
@pelusch Hi! Try with:
ActualsCumulativeTotalYTD =
VAR MaxKategori = MAX(NAV[Account Category Code])
RETURN
IF(
NOT(ISINSCOPE(NAV[Account Subcategory Code])),
CALCULATE(
SUMX(
CALCULATETABLE(
SUMMARIZE(NAV, NAV[Account Category Name], "Utfall", [Amount_YTD]),
FILTER(
ALLSELECTED(NAV),
NAV[Account Category Code] <= MaxKategori
)
),
[Amount_YTD]
),
FILTER(ALLSELECTED(NAV), NAV[Account Category Code] <= MaxKategori)
),
[Amount_YTD]
)
if it's ok accept my answer as solution, instead please provide some sample data on which you calculate the measure and the expected output.
BBF
This it the expected output of the data above when I use slicers 'Date'[Year] = 2024 and 'Date'[Month] = 2.
Account Category Name | Accoubt Subcategory Name | ActualsCumulativeTotalMTD | ActualsCumulativeTotalYTD |
Totala Intäkter | Försäljning | 500 | 1100 |
Total | 500 | 1 100 | |
Bruttovinst | Varukostnad | -100 | -300 |
Total | 400 | 800 | |
Driftresultat | Personalkostnad | -350 | -750 |
Förbrukningskostnader | -100 | -250 | |
Övriga Kostnader | -50 | -90 | |
Total | -100 | -290 | |
Total | -100 | -290 |
@pelusch Try with:
When I use this formula it does only consider the filtered month on the [Account Category Name]-level.
It's so strange because then I use [Account Category Code] as a row then the calculations makes it perfect (both the MTD and YTD calculation with cumulative subtotals). But when I use [Account Category Name] it does not work.
I also use exactly the same code:
@pelusch ok, i've done a fix:
Thanks but not working 😞 I have been working for three days to solve this problem and I don't think there is an issue with the data model.
@pelusch For me it's working, in the screen the first measure, wrong, and the second one corresponding to the desided output:
what's wrong?
BBF
Now I have looked through your file. From my point of view, the calculation are doing fine when month is not selected. But when I select a month as nr 2 for instance, I'm getting the wrong calculations . In your screenshot it looks like you have selected the filter (?).
This reminds me of my main issue, I note that if I don't select a month, the YTD-calculation are doing just fine, but in this case I want the selected month to be the last month of the YTD, if that makes sense.
See the differences in the screenshots below:
This is the expected output for month filter on 2:
Account Category Name | Accoubt Subcategory Name | ActualsCumulativeTotalMTD | ActualsCumulativeTotalYTD |
Totala Intäkter | Försäljning | 600 | 1 100 |
Total | 600 | 1 100 | |
Bruttovinst | Varukostnad | -200 | -300 |
Total | 400 | 800 | |
Driftresultat | Personalkostnad | -400 | -750 |
Förbrukningskostnader | -150 | -250 | |
Övriga Kostnader | -40 | -90 | |
Total | -190 | -290 | |
Total | -190 | -290 |
@pelusch here the measure adjusted:
Thanks, it works fine when you select month nr 2. But when I select month nr 1 (see below), the calculation are not correct if you're looking at the [Account Category Name] level. Sorry if I didn't made that clear for you.
When you select month nr 1, my expected output are:
Account Category Name | Accoubt Subcategory Name | ActualsCumulativeTotalMTD | ActualsCumulativeTotalYTD |
Totala Intäkter | Försäljning | 500 | 500 |
Total | 500 | 500 | |
Bruttovinst | Varukostnad | -100 | -100 |
Total | 400 | 400 | |
Driftresultat | Personalkostnad | -350 | -350 |
Förbrukningskostnader | -100 | -100 | |
Övriga Kostnader | -50 | -50 | |
Total | -100 | -100 | |
Total | -100 | -100 |
@pelusch try with this new version:
no one needs to apologize, we are working together 🙂
BBF
Hey, you are awesome at this. Great job, It works! The datekey has now been YearMonth but in my datamodel a Date should fit better. I tried to import my original Date table and changed the key in the pbix-file that you sent me and noted that the formula didn't fit, wasn't expecting that :(. Perhaps you can take a look at this? 🙂 I take a chance and mailing the file back to you.
//Per
@pelusch Hi! You have still my Date table in the filters, without relation to NAV, so they don't work. By changing filters with Query1 fields, they works, but the problem is that max year in Query1 is 2017, and NAV has only 2024 datas. So you have to have the same year both in Query1 and NAV to make it work.
Please accept my last message as a solution, to help other users with formula.
BBF
My bad, I only imported the top 1000 rows from Date table by mistake. I tried to change the filter with Query1 fields - but is this possible?
The filter then looks like this:
@pelusch i'm sending you the last pbix. I've fixed the amount_ytd measure and the last one. I think it's working but let me know if it is correct.
BBF
Of some reason, I don't get the same output as you so I think I have some big problems here. I have created a table with the testdata and don't manage to get the same figures. If I am only using the 'Table' without any other relationships and picks the [Year] and [Month] as a slicer I get wrong results.
User | Count |
---|---|
25 | |
12 | |
8 | |
6 | |
6 |
User | Count |
---|---|
26 | |
12 | |
11 | |
9 | |
6 |