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
kadingo
Helper II
Helper II

How to add WHERE condition for last 12 months in DAX query

DAX.jpg

 

I have the above sample DAX expression to get product sales data. I want to add some sort of a WHERE clause such that only records where the "[Sale-Date]" is in the last 12 months are returned.

I cant seem to figure out how to add that condition, any one please help.

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @kadingo 

I see you have got a good solution from another site.

Capture1.JPG

It seems you use DAX query in SQL Server Management Studio or open source tools like DAX studio.

https://docs.microsoft.com/en-us/dax/dax-queries

As tested, in Power BI desktop, EVALUATE() function  doesn't support.

You can use calculatetable or calculate funtion to replace.

 

If you use Power BI Desktop, want to show table visual which meets your condition, you could create measures as belwo:

One method is:

when you have "sales" column, you can make the table visual to show sales rows which meets the condtion:

Measure =
CALCULATE (
    SUM ( Product_Category[sales] ),
    FILTER (
        Product_Category,
        Product_Category[Acronym] = "Test Product"
            && Product_Category[Sales-Date]
                >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

Capture2.JPG

Method two:

If you have no "sales" column, you could add the [measure 2] in the visual level filter of the table visual.

Measure 2 =
IF (
    MAX ( Product_Category[Acronym] ) = "Test Product"
        && MAX ( Product_Category[Sales-Date] )
            >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) ),
    1,
    0
)

Capture3.JPG

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

View solution in original post

7 REPLIES 7
v-juanli-msft
Community Support
Community Support

Hi @kadingo 

I see you have got a good solution from another site.

Capture1.JPG

It seems you use DAX query in SQL Server Management Studio or open source tools like DAX studio.

https://docs.microsoft.com/en-us/dax/dax-queries

As tested, in Power BI desktop, EVALUATE() function  doesn't support.

You can use calculatetable or calculate funtion to replace.

 

If you use Power BI Desktop, want to show table visual which meets your condition, you could create measures as belwo:

One method is:

when you have "sales" column, you can make the table visual to show sales rows which meets the condtion:

Measure =
CALCULATE (
    SUM ( Product_Category[sales] ),
    FILTER (
        Product_Category,
        Product_Category[Acronym] = "Test Product"
            && Product_Category[Sales-Date]
                >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

Capture2.JPG

Method two:

If you have no "sales" column, you could add the [measure 2] in the visual level filter of the table visual.

Measure 2 =
IF (
    MAX ( Product_Category[Acronym] ) = "Test Product"
        && MAX ( Product_Category[Sales-Date] )
            >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) ),
    1,
    0
)

Capture3.JPG

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

Anonymous
Not applicable

Hi @kadingo,

 

Before all that expression use calculatetable() and apply the filter as it were a measure.

Anonymous
Not applicable

You have taken this question from StackOverflow. Stop spamming everywhere with the same questions.

Regards,

Lewis

In my opinion, trying in two different technical sites might not necessariy be spamming. Look, finally i have got an answer on StackOverflow. And now am posting the answer here on this thread to help a future reader with a similar problem, how can this be spamming. 

 

In the past, i have posted a question on both stackoverflow and here, and the solution in some cases has come from here but not on stackoverflow. So i know i have two very good sites with technically talented people but i can not tell in advance where the solution will come from.

 

Well, you can call it spamming but whats important to me is getting a solution to a problem i have at hand than what some one is thinking. Not every thought is correct.

Anonymous
Not applicable

Hi @kadingo ,

 

Had to try but calculatetable() won't work? Seems like a plausible possibility.

 

Let me know if it worked.

 

BR,

DR

Tahreem24
Super User
Super User

@kadingo ,

 

To calculate last 12 months sales use the below measure:

Last Year sales = CALCULATE(SUM('Table'[SalesColumn]),PREVIOUSYEAR('Table'[DateCOlumn]))

 

Don't forget to hit Thumbs up and accept this as a solution if you find it helpful! 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

I dont want to calculate the sum, i want to return raws / records for the last 1 year (12 months)

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.