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
CL7777
Helper III
Helper III

creating a measure that sums another measure that fills in data missing on a table

Hi all,

I consider myself an intermediate DAX user but I am struggling with this one.. In the table below, I created a measure to calculate the missing QOH values with the last non blank value that occurred in time. So, for example, for part A and plant FL, the value of this measure for the missing QOH for Feb through Apr of 2020 would be 2 (the last non blank value that occurred in time).

 

End of Monthpart numberplantQOH
1/31/2020AFL2
2/28/2020AFL 
3/31/2020AFL 
4/30/2020AFL 
5/31/2020AFL4
6/30/2020AFL6
7/31/2020AFL 
8/31/2020AFL 
9/30/2020AFL 
10/31/2020AFL 
11/30/2020AFL 
12/31/2020AFL10
1/31/2020ACA1
2/28/2020ACA2
3/31/2020ACA 
4/30/2020ACA 
5/31/2020ACA5
6/30/2020ACA 
7/31/2020ACA 
8/31/2020ACA8
9/30/2020ACA 
10/31/2020ACA 
11/30/2020ACA 
12/31/2020ACA2
1/31/2020BFL11
2/28/2020BFL 
3/31/2020BFL 
4/30/2020BFL 
5/31/2020BFL13
6/30/2020BFL 
7/31/2020BFL 
8/31/2020BFL 
9/30/2020BFL4
10/31/2020BFL5
11/30/2020BFL 
12/31/2020BFL8
1/31/2020BCA22
2/28/2020BCA 
3/31/2020BCA24
4/30/2020BCA11
5/31/2020BCA 
6/30/2020BCA 
7/31/2020BCA 
8/31/2020BCA13
9/30/2020BCA 
10/31/2020BCA 
11/30/2020BCA6
12/31/2020BCA7

 

 

 

Here is the measure that I created to do this:

 

xpredateQOH =
VAR currentpart = MAX('tabletouse'[Part Number])
VAR currentplant = MAX('tabletouse'[Plant])
VAR currentdate = MAX('tabletouse'[Last Day of Month])
VAR predate = CALCULATE (
                               MAX ( 'tabletouse'[Last Day of Month] ),
                              FILTER
                                    (ALL ( 'tabletouse' ),
                                         'tabletouse'[Part Number] = currentpart
                                   && 'tabletouse'[Plant] = currentplant
                                   && 'tabletouse'[Last Day of Month] <= currentdate
                                   && NOT ( ISBLANK ( 'tabletouse'[QOH] ) )
                                  && NOT ( ISBLANK ( 'tabletouse'[Cost] ) )
                        )
               )
RETURN
     CALCULATE (
              MAX ( 'tabletouse'[QOH] ),
              FILTER
                  (ALL ( 'tabletouse' ),
                            'tabletouse'[Part Number] = currentpart
                      && 'tabletouse'[Plant] = currentplant
                      && 'tabletouse'[Last Day of Month] = predate
                      && NOT ( ISBLANK ( 'tabletouse'[QOH] ) )
                      && NOT ( ISBLANK ( 'tabletouse'[Cost] ) )
               )
   )
 
*********************
my problem comes in when I want to use this measure in a measure.. For example, I want to sumx over a subset of date values the values of the above measure for xpredateQOH.. In other words something like: SUMX(tableofDates, xpredateQOH). It does not work.. this is because it doesnt recognize the fact that each of the plants and part numbers have their own xpredateQOH.. If I select a specific plant and a partnumber from a slicer, the calculation works fine, but when I try to sum the xpredateQOH without specifying a particular plant and part number, it picks the max plant and max part number (in the whole data set) and calculates everything based on that part and plant. I want it to add (sumx) over those date values the total xpredateQOH for each part for each plant separately and add those together if there is no filters on them. I want to get an annual total quantity on hand of all parts in all plants.
 
any help would be much appreciated, I have spend an inordinate amount of time on this already.
 
1 ACCEPTED SOLUTION
DataZoe
Employee
Employee

@CL7777 Please try these measures:

 

QOH With Missing =
VAR _d =
    MAX ( 'Table'[End of Month] )
VAR _p =
    MAX ( 'Table'[plant] )
VAR _s =
    MAX ( 'Table'[part number] )
RETURN
    IF (
        ISBLANK ( SUM ( 'Table'[QOH] ) ),
        CALCULATE (
            LASTNONBLANKVALUE ( 'Table'[End of Month], SUM ( 'Table'[QOH] ) ),
            REMOVEFILTERS ( 'Table' ),
            'Table'[End of Month] < _d,
            'Table'[plant] = _p,
            'Table'[part number] = _s
        ),
        SUM ( 'Table'[QOH] )
    )

 
 
then to get the total with the filled in values:
 
Total QOH with Missing = SUMX('Table',[QOH With Missing])
 
DataZoe_1-1610672296570.png
 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

View solution in original post

4 REPLIES 4
CL7777
Helper III
Helper III

Hi DataZoe,

I have a quick followup with this post. My model is running very slow with this code, because I have a measure nested in an iterator. and the file is large (2 million rows). Is there a way to do this without putting a measure into an iterator?

 

@CL7777 This is a good point! Usually at this point I would see if I could push the problem back towards the source. I'm not sure how to do this in Power Query (M), which is one step back. So I'd go another step back, what is the source of the 2 million rows of data? Is it a SQL database?

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

DataZoe
Employee
Employee

@CL7777 Please try these measures:

 

QOH With Missing =
VAR _d =
    MAX ( 'Table'[End of Month] )
VAR _p =
    MAX ( 'Table'[plant] )
VAR _s =
    MAX ( 'Table'[part number] )
RETURN
    IF (
        ISBLANK ( SUM ( 'Table'[QOH] ) ),
        CALCULATE (
            LASTNONBLANKVALUE ( 'Table'[End of Month], SUM ( 'Table'[QOH] ) ),
            REMOVEFILTERS ( 'Table' ),
            'Table'[End of Month] < _d,
            'Table'[plant] = _p,
            'Table'[part number] = _s
        ),
        SUM ( 'Table'[QOH] )
    )

 
 
then to get the total with the filled in values:
 
Total QOH with Missing = SUMX('Table',[QOH With Missing])
 
DataZoe_1-1610672296570.png
 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

Worked like a charm.. thank you SO much

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.