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
dandreev
Helper I
Helper I

Advise on optimizing multiple DAX measures

Hello,
I have built a table with DAX Measures. 

Product5/1/20236/1/20237/1/20233 MonthsTotalAvgVariance
A11105261313
B122015471631
C133025581939
Total 3660551314883

 

I have the following column measures for the months:
 

 

Mnth1 = 
VAR Month1Total =
    CALCULATE (
        DISTINCTCOUNT ( 'Product'[ProductID] ),
        //KEEPFILTERS ( VALUES ( 'DimDate'[YearMonth] ) ),
        AND (
            'DimDate'[Date]
                >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ) - 1, 1 ),
            'DimDate'[Date] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 )
        )
    )
RETURN
    IF ( ISBLANK ( Month1Total ), 0, Month1Total )

 

Second Month Column

 

Mnth2 = 
VAR Month2Total =
    CALCULATE (
        DISTINCTCOUNT ( 'Product'[ProductID] ),
        //KEEPFILTERS ( VALUES ( 'DimDate'[YearMonth] ) ),
        AND (
            'DimDate'[Date]
                >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ) - 2, 1 ),
            'DimDate'[Date] < DATE ( YEAR ( TODAY () ), MONTH ( TODAY () )-1, 1 )
        )
    )
RETURN
    IF ( ISBLANK ( Month2Total ), 0, Month2Total )

 

My model is classic star model with one fact table and 4 dienssion tables and Date table.

I created another matrix visual with calculation groups, but report's requstors didn't like the extra row that was added to column headers. That was the reason I went to building a measure for each column. My questions are
How can I combine the measures? The original table has over 15 columns. 
Also, is there a way to create measure names that will change there names based on current date? For example, if today is September the headears chnge from July to August and June to July, etc.
Thank you for your time!

 

1 ACCEPTED SOLUTION

I built a view in SQL server and resolve the problem. My SQL skills are better than my DAX skills. Thank you to everyone who helped or read the ticket.

View solution in original post

3 REPLIES 3
Sahir_Maharaj
Super User
Super User

Hello @dandreev,

 

1. Instead of creating separate measures for each month, you can use Time Intelligence functions like TOTALYTD and SAMEPERIODLASTYEAR to calculate the total and the same month's data for each month.

TotalQty = 
VAR SelectedDate = TODAY()
VAR PreviousMonthEndDate = EOMONTH(SelectedDate, -1)
VAR TwoMonthsAgoEndDate = EOMONTH(SelectedDate, -2)
VAR ThreeMonthsAgoEndDate = EOMONTH(SelectedDate, -3)

RETURN
SWITCH(TRUE(),
    SELECTEDVALUE('DimDate'[Date]) = SelectedDate, CALCULATE(DISTINCTCOUNT('Product'[ProductID])),
    SELECTEDVALUE('DimDate'[Date]) = PreviousMonthEndDate, CALCULATE(DISTINCTCOUNT('Product'[ProductID]), DATESYTD('DimDate'[Date])),
    SELECTEDVALUE('DimDate'[Date]) = TwoMonthsAgoEndDate, CALCULATE(DISTINCTCOUNT('Product'[ProductID]), SAMEPERIODLASTYEAR(DATESYTD('DimDate'[Date]))),
    SELECTEDVALUE('DimDate'[Date]) = ThreeMonthsAgoEndDate, CALCULATE(DISTINCTCOUNT('Product'[ProductID]), SAMEPERIODLASTYEAR(SAMEPERIODLASTYEAR(DATESYTD('DimDate'[Date])))),
    0
)

2. To dynamically change the measure names based on the current date, you can use DAX variables and concatenate them in the measure names.

DynamicMeasureName = 
VAR SelectedDate = TODAY()
VAR Month1 = FORMAT(EOMONTH(SelectedDate, -1), "MMM")
VAR Month2 = FORMAT(EOMONTH(SelectedDate, -2), "MMM")
VAR Month3 = FORMAT(EOMONTH(SelectedDate, -3), "MMM")

RETURN
SWITCH(SELECTEDVALUE('DimDate'[Date]),
    SelectedDate, "Qty " & Month1,
    EOMONTH(SelectedDate, -1), "Qty " & Month2,
    EOMONTH(SelectedDate, -2), "Qty " & Month3,
    BLANK()
)

 

Should you require further assistance please do not hesitate to reach out to me.

 

 


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Hi Sahir,
Thank you for your time!
I have hard time to make the solution work. DynamicMeasureName is returning blank. I think the reason is mismatch of the data type. 
TotalQty measure is also returning blank. 
Thank you!

I built a view in SQL server and resolve the problem. My SQL skills are better than my DAX skills. Thank you to everyone who helped or read the ticket.

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.