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

Subset of "Sales" table including only latest 3 months data

I'm studying Power BI by the official sample "Contoso Sales for Power BI Designer.pbix". https://www.microsoft.com/en-us/download/details.aspx?id=46801


How do I create a measure which is the subset of "Sales" table including only latest 3 months data?

 

sales-table.png

 

Thank you.

 

 

1 ACCEPTED SOLUTION

OK, your formula is excellent. If you were creating a table, it would be exactly what you want. But, if you truly want a measure, you need to aggregate over the table you have created. For example:

 

Sales 3-months = 
    SUMX(
        FILTER(
            'Sales', 
            'Sales'[DateKey] >= 
                DATE(
                    YEAR(MAX('Sales'[DateKey])), 
                    MONTH(MAX('Sales'[DateKey]))-3, 
                    DAY(MAX('Sales'[DateKey]))
                )
        ),
        [SalesAmount]
    )

Honestly, very nice. I was not super clear in my previous response and you executed on it very well.  Now, you could potentially run into issues with the -3 on month in the case that the last month happens to be January, February or March. You could also perhaps just set DAY to 1, just really depends on what you are going for exactly. Anyway, you are definitely on the right track.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

6 REPLIES 6
niteshtrehan89
Helper III
Helper III

Hi Mackie,

use the below measure.

 

sales = CALCULATE(SUM(Table1[sale]),DATESINPERIOD(Table1[Date],LASTDATE(Table1[Date]),-3,MONTH))

Greg_Deckler
Super User
Super User

FILTER. Should be able to get the MAX Date in the table and then you could calculate 3 months before that and filter between those dates.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I created a measure using this:

 

Sales 3-months = FILTER('Sales', 'Sales'[DateKey] >= DATE(YEAR(MAX('Sales'[DateKey])), MONTH(MAX('Sales'[DateKey]))-3, DAY(MAX('Sales'[DateKey]))))

 

But this returns an error. Can I borrow your help?  @Greg_Deckler 

Sure @Mackie just listening to virtual MVP events and all the bars are closed in Ohio so I have nothing else to do!! 🙂

 

I'm downloading the file now and will take a look.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

OK, your formula is excellent. If you were creating a table, it would be exactly what you want. But, if you truly want a measure, you need to aggregate over the table you have created. For example:

 

Sales 3-months = 
    SUMX(
        FILTER(
            'Sales', 
            'Sales'[DateKey] >= 
                DATE(
                    YEAR(MAX('Sales'[DateKey])), 
                    MONTH(MAX('Sales'[DateKey]))-3, 
                    DAY(MAX('Sales'[DateKey]))
                )
        ),
        [SalesAmount]
    )

Honestly, very nice. I was not super clear in my previous response and you executed on it very well.  Now, you could potentially run into issues with the -3 on month in the case that the last month happens to be January, February or March. You could also perhaps just set DAY to 1, just really depends on what you are going for exactly. Anyway, you are definitely on the right track.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you for your detailed explanation. I tried your measure and understood these things:
- If I want to create an aggregation, I should create a measure.
- If I want to create just a subset, I should create a table. Not a measure.

 

By the way, in Japan, everyone wears a face mask everywhere. That's a strange sight..
I hope things settle down soon.

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.

Top Solution Authors