Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TReynolds
Helper I
Helper I

Problems getting total budget using FILTER, SUMX, and DISTINCT

Hi,

 

I'm trying to calculate the total budget for the months prior to a selected date (the measure [date selected]). Here's an example of the data I'm using. 

 

DateProductProdTypePlantBudget
2020-09-01DogsLargeBark2
2020-09-01DogsMediumBark5
2020-09-01DogsSmallBark3
2020-09-01CatsCatsMeow7
2020-08-02DogsLargeBark5
2020-08-02DogsMediumBark10
2020-08-02DogsSmallBark3
2020-08-02CatsCatsMeow10
2020-08-01DogsLargeBark5
2020-08-01DogsMediumBark10
2020-08-01DogsSmallBark3
2020-08-01CatsCatsMeow10
2020-07-02DogsLargeBark5
2020-07-02DogsMediumBark5
2020-07-02DogsSmallBark3
2020-07-02CatsCatsMeow2
2020-07-01DogsLargeBark5
2020-07-01DogsMediumBark5
2020-07-01DogsSmallBark3
2020-07-01CatsCatsMeow2
2020-06-02DogsLargeBark5
2020-06-02DogsMediumBark5
2020-06-02DogsSmallBark3
2020-06-02CatsCatsMeow2
2020-06-01DogsLargeBark5
2020-06-01DogsMediumBark5
2020-06-01DogsSmallBark3
2020-06-01CatsCatsMeow2

 

Here's the DAX I am using:

 

Monthly Buget Sum =
VAR _selectmonth = MONTH([select date])
VAR _previousmonth = _selectmonth - 1
VAR _selectyear = YEAR([select date])
RETURN
CALCULATE(
SUMX( DISTINCT('data table'[Budget]), 'data table'[Budget]),
FILTER(
ALL('data table'),
'data table'[Date].[Date] >= DATE(2020-06-01)
&& 'data table'[Date] <= _previousmonth
&& 'data table'[Plant} = MIN('data table'[Plant])
)
)

 

 

 

If I choose 2020-06-02 for [select date], Monthly Budget Sum returns (blank) for both plants, Bark and Meow. This is expected because there is no month before 06.

 

If I choose 2020-07-01 for [select date], Monthly Budget Sum returns 13 for Bark and 2 for Meow. This is correct, because the monthly budget for Bark is 3 Small dogs, 5 Medium and 5 Large. The monthly budget for Meow is 2.

 

Things get weird when I select 2020-08-02. Monthly Budget Sum returns 4 for Meow, which is correct (monthly budget for 06 was 2 and for 07 was 2). For Bark, Monthly Budget Sum returns 23, which is incorrect (monthly budget for 06 was 13 and for 07 was 13). Monthly Budget Sum dropped the 3 (the monthly budget for Small Dogs). 

 

When I set [select date] to 2020-09-01, Monthly Budget Sum returns 14 for Meow factory (which is correct, 2 + 2 + 10) and 41 for Bark factory (which is incorrect 13 + 10 (the wrong budget) + 18). 

 

Why does Power BI report a correct monthly budget for 06 and 08 but not for 07 for the factory Bark?

 

Any help that can be given to solve this issue, or rewrite my DAX (I've enough DAX to be dangerous, but that's it), would be greatly appreciated. 

 

Also, is there another way to differentiate between categories in FILTER besides using MAX() or MIN()? Without one expression or the other, Monthly Budget Sum returns the summated monthly budgets for all factories and does not break it down by category (i.e., by Bark and Meow).  

 

Thanks for your help,

 

T

 

 

2 REPLIES 2
amitchandak
Super User
Super User

@TReynolds , Have tried it with Time Intelligence and Date table. Also, use Values in place of distinct in Budget Sumx

 

Budget Measure =SUMX( Values('data table'[Budget]), 'data table'[Budget])

 

MTD Sales = CALCULATE([Budget Measure],DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE([Budget Measure],DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE([Budget Measure],previousmonth('Date'[Date]))
last MTD (complete) Sales = CALCULATE([Budget Measure],DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-1,MONTH))))
previous month value = CALCULATE([Budget Measure],previousmonth('Date'[Date]))

diff = [MTD Sales]-[last MTD Sales]
diff % = divide([MTD Sales]-[last MTD Sales],[last MTD Sales])

 

Power BI — MTD
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...


Appreciate your Kudos.

 

smillar
New Member

Why don't you create a calculated table, or have the database query do it?  It would be trivial at the database level.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.