cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
PBNikr Regular Visitor
Regular Visitor

Sum of last n days in DAX (measure)

Hey!

 

I am new to Power BI and struggling to get this DAX code to show me the correct values. I would like to view the following as aggregated numbers (separate cards). Note that I am using a live connection to a cube:

 

- Users from start

- Users last 30 days

- Users last 7 days

- Users previous day

 

The code that I have for Users from start is the following:

 

Users from start = 
    CALCULATE (
        'Users'[Amount];
        ALL('Date'[Date]))

 

I have tried to retrieve the users from the last 30 days, but I'm pretty sure there is a better way of doing this:

 

Users 30 days = 
CALCULATE (
    SUM ( 'Users'[AmountUsers] );
    FILTER (
        ALL ( 'Date'[Date] );
        'Date'[Date]
            >= MAX (  'Date'[Date] ) - 30
            &&  'Date'[Date] <= MAX (  'Date'[Date] )
    )
)

Any idea on how I can retrieve the correct data for the different n-days with a good DAX code? 

 

Also:

Something I noticed is that when I use the SUM() function I can't use the same fields in the calculation that are in the Users table. It looks like it can only handle hidden values. Any explanation as to why this is?

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PBNikr Regular Visitor
Regular Visitor

Re: Sum of last n days in DAX (measure)

@Greg_Deckler Your right, thanks! This worked for me:

 

Users 30 days = 
CALCULATE (
    SUM ( 'Users'[AmountUsers] );
    FILTER (
        ALL ( 'Date'[Date] );
        'Date'[Date]
            > TODAY() - 30
    )

 

I couldn't figure out why I was receiving blank values but it looks like the most recent data hasn't been loaded into the test cube. Problem solved!

View solution in original post

2 REPLIES 2
Highlighted
Super User IV
Super User IV

Re: Sum of last n days in DAX (measure)

Actually, I don't think your DAX code is half-bad considering you are connected to live to a cube. I'm not sure why you have that && clause in there as I can't think of under what circumstance would ever have a date in your cube that would somehow be greater than the max date in the cube, so it's kind of unnecessary. You could also potentially use TODAY() - 30 instead of MAX() - 30 but overall you have the right idea. 


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

PBNikr Regular Visitor
Regular Visitor

Re: Sum of last n days in DAX (measure)

@Greg_Deckler Your right, thanks! This worked for me:

 

Users 30 days = 
CALCULATE (
    SUM ( 'Users'[AmountUsers] );
    FILTER (
        ALL ( 'Date'[Date] );
        'Date'[Date]
            > TODAY() - 30
    )

 

I couldn't figure out why I was receiving blank values but it looks like the most recent data hasn't been loaded into the test cube. Problem solved!

View solution in original post

Helpful resources

Announcements
Coming Soon: T-Shirt Design Contest

Coming Soon: T-Shirt Design Contest

Keep your eyes open for our upcoming T-shirt design contest!

Meet the 2020 Season 1 Power BI Super Users!

Meet the 2020 Season 1 Power BI Super Users!

It’s the start of a new Super User season! Learn all about the new Super Users and brand-new tiered recognition system.

Super User Challenge: Can You Solve These?

Super User Challenge: Can You Solve These?

We're celebrating the start of the New Super User season with our first ever Super User 'Can You Solve These?' challenge.

Power BI Desktop Update - February 2020

Power BI Desktop Update - February 2020

We are super excited for our update this month, as we are releasing two of our top community requests!

Power Platform Online Conference

Power Platform Online Conference

Join us for the first ever Power Platform Online Conference!

Top Solution Authors