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
manojsv20
New Member

DAX Measure to get the previous days count if there is no data for the current day

Hello All,

 

I have a request to create following Power BI table visual. Have included the input data table we are getting from the source and the input data will have values only for the date if there is any change in the items cleared.

Challege here is to create the table visual with all days even though there is no data available for those days in Input table.  Have used the Date table and join that with the input table. Was successful in creating the Total, Total YTD cleared using TOTALYTD dax function. 

However, I wanted to create a DAX measure for the Inventory column. If there is no data for the corresponding day DAX measure has to refer the previous day counts where it has the data in the input table and show it in Power BI table visual. Please refer the Output Power BI table visual and Input table for more information.

 

I would really appreciate for any suggestions or input in resolving this issue.

 

Output - Power BI Table Visual
DateTotalTotal YTD ClearedInventory
1/1/2020500005000
1/2/202052002005000
1/3/202052005004900
1/4/202052005004900
1/5/202052005004900
1/6/202052005004900
1/7/202052005004900
1/8/202052005004900
1/9/202052005004900
1/10/202052008004700

 

Input Data
DateTotalClearedInventory
1/1/2020500005000
1/2/202052002005000
1/3/202052003004900
1/10/202052005004700

 

Thank you!!

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

Hi, @manojsv20 

 

Based on my research, you may create measures as follows.

 

Inventory measure = 
var _date = 
CALCULATE(
            MAX('Input Data'[Date]),
            FILTER(
                ALLSELECTED('Input Data'),
                'Input Data'[Date]<MAX(DateTable[Date])
            )
)
return
IF(
    ISBLANK(MAX('Input Data'[Total])),
    LOOKUPVALUE('Input Data'[Total],'Input Data'[Date],_date)-
    LOOKUPVALUE('Input Data'[Cleared],'Input Data'[Date],_date),
    MAX('Input Data'[Total])-MAX('Input Data'[Cleared])
)

Total YTD Cleared = TOTALYTD(SUM('Input Data'[Cleared]),DateTable[Date])

Total measure = [Inventory measure]-[Total YTD Cleared]

 

 

Result:

b1.png

 

Best Regards

Allan

 

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

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @manojsv20 

 

If you take the answer of someone, please mark it as the solution to help the other members who have same problems find it more quickly. If not, let me know and I'll try to help you further. Thanks.

 

Best Regards

Allan

v-alq-msft
Community Support
Community Support

Hi, @manojsv20 

 

Based on my research, you may create measures as follows.

 

Inventory measure = 
var _date = 
CALCULATE(
            MAX('Input Data'[Date]),
            FILTER(
                ALLSELECTED('Input Data'),
                'Input Data'[Date]<MAX(DateTable[Date])
            )
)
return
IF(
    ISBLANK(MAX('Input Data'[Total])),
    LOOKUPVALUE('Input Data'[Total],'Input Data'[Date],_date)-
    LOOKUPVALUE('Input Data'[Cleared],'Input Data'[Date],_date),
    MAX('Input Data'[Total])-MAX('Input Data'[Cleared])
)

Total YTD Cleared = TOTALYTD(SUM('Input Data'[Cleared]),DateTable[Date])

Total measure = [Inventory measure]-[Total YTD Cleared]

 

 

Result:

b1.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

sturlaws
Resident Rockstar
Resident Rockstar

Hi @manojsv20 

 

you will have to write three measures to achieve this, like this:

MeasureTotal =
VAR _d =
    CALCULATE ( SELECTEDVALUE ( dates[Date] ) )
VAR _lastDateInInput =
    CALCULATE ( MAX ( data[Date] ); ALL ( data ) )
VAR _dMax =
    CALCULATE ( MAX ( data[Date] ); FILTER ( ALL ( data ); data[Date] <= _d ) )
VAR _total =
    IF (
        _d <= _lastDateInInput;
        CALCULATE ( SUM ( data[Total] ); FILTER ( ALL ( dates ); dates[Date] = _dMax ) );
        BLANK ()
    )
RETURN
    _total

 

I created a sample report based on the data you provided: report

 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

 

Hi @sturlaws,

 

Thanks a lot for your response!!

 

I checked the report you shared. All looks good, but the Measure total value didn't show the correct value. Count expected from 1/3 to 1/9 is 4400, but the report shows 5200. Count for 1/10 has to be 3700.

 

Would appreciate any suggestions to resolve this issue

 

DateMeasureInventoryMeasureClearedMeasureTotal
1/1/2020 0:00500005000
1/2/2020 0:0050002005200
1/3/2020 0:0049005005200
1/4/2020 0:0049005005200
1/5/2020 0:0049005005200
1/6/2020 0:0049005005200
1/7/2020 0:0049005005200
1/8/2020 0:0049005005200
1/9/2020 0:0049005005200
1/10/2020 0:00470010005200

 

Sorry, thought the Output table you provided was the desired output. Try rewriting the measure to this:

MeasureTotal = [MeasureInventory]-[MeasureCleared]

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.