Forum Discussion

Petanek333's avatar
Petanek333
Icon for Helper III rankHelper III
4 years ago
Solved

DAX Measure summing values between today and selected date

Hi, 

I would greatly appreciate it if you could help me with creating a measure. 

I have an import from an SQL database (Table 1) that gives me a stock quantity of various product IDs at the time of refresh. So I can see the actual amount. But I have no option to look into history in given table, the data is being overwritten each time. I have another table(s) though, that store stock changes of those products. So I know that the day before there was a +2 change for product A, a week ago there was a -3 change of product B etc. See the picture below: 

 

 

 

 

 

 

 

My goal is to see into the past 🙂 I simply want to know what was the stock quantity of product B on June, 9th for example [66 (today) + 1 + 4 - 4). 

In other words, I would like to select a date, for example 1.7.2022, and dax would have to do a calculation like: today's quantity from table 1 + sum of values of table 2 and 3 for 20.7.2022 (yesterday's date) + sum of values of table 2 and 3 for 19.7.2022 + ... + sum of values of table 2 and 3 for 1.7.2022. 

I mostly struggle with defining what dates dax should choose. If I select a date, it has to sum the actual quantity and all of the changes between today and the selected day.

Can you please help me with this?

  • Hi,

    I am not sure how your data model looks like, but I tried to create a sample pbix file like below.

    I suggest having a calendar table.

    Please check the below picture and the attached pbix file.

     

     

    Quantity measure: = 
    IF (
        MAX ( 'Calendar'[Date] ) <= TODAY (),
        SUM ( Data[Quantity] )
            + CALCULATE (
                SUM ( 'Table One'[Change] ),
                'Table One'[Date] >= MAX ( 'Calendar'[Date] )
            )
            + CALCULATE (
                SUM ( 'Table Two'[Change] ),
                'Table Two'[Date] >= MAX ( 'Calendar'[Date] )
            )
    )

2 Replies

  • Hi,

    I am not sure how your data model looks like, but I tried to create a sample pbix file like below.

    I suggest having a calendar table.

    Please check the below picture and the attached pbix file.

     

     

    Quantity measure: = 
    IF (
        MAX ( 'Calendar'[Date] ) <= TODAY (),
        SUM ( Data[Quantity] )
            + CALCULATE (
                SUM ( 'Table One'[Change] ),
                'Table One'[Date] >= MAX ( 'Calendar'[Date] )
            )
            + CALCULATE (
                SUM ( 'Table Two'[Change] ),
                'Table Two'[Date] >= MAX ( 'Calendar'[Date] )
            )
    )