Forum Discussion

J94's avatar
J94
Advocate II
1 year ago
Solved

Fill blanks with previous values

Hi everyone, 

 

I want to show the sales per week. When there are no sales made for a week, I want to show the sales from the week before (or the week before that, etc. when there are sales made again. 


I created a simple sample dataset:

WeekBrandProduct_bkSales
1AAA110
1CCA150
1BBA125
2CCB2100
2BBB245
2AAB220
3CCC3150
3BBC365
3AAC330
4CCD4 
4BBD4250
4AAD4200
5CCE5 
5BBE5 
5AAE5 
6CCF6 
6BBF6 
6AAF6100


I had this measure for Brand level (based on a similar topic already asked: https://community.fabric.microsoft.com/t5/Desktop/Fill-blank-values-with-previous-value/td-p/1210628๐Ÿ˜ž

Sales (fillblanks) =
VAR _weekwithsales =
    CALCULATE(MAX('Tabel'[Week]),
    FILTER(
        ALLEXCEPT('Tabel', Tabel[Brand]),
        'Tabel'[Week]<=MAX('Tabel'[Week]) && 'Tabel'[Sales]<>BLANK()
    )
    )
RETURN
CALCULATE(
    SUM([Sales]),
    'Tabel'[Week] = _weekwithsales, ALLEXCEPT('Tabel', Tabel[Brand])
)

This works, but I need it on Product_bk level. And that doesn't work, because the table doesn't have an empty row for every week for Product_bk where there are no sales. I did try to add blank rows for every week in Query Editor, but because the real dataset is larger, it wasn't able to handle that request. 
So, is there a way to change the measure for Product_bk? 
  • J94 Well, yeah, because in your data you don't have any previous sales for the products that are blank. You are basically running into an Auto Exist issue. Essentially you are attempting to invent data that doesn't exist. The measure is not going to calculate on rows it feels are invalid. They were supposed to have fixed the auto exist issue with allowing you to select "Independent" as the "Value filter behavior" for the semantic model but it doesn't work in all cases, like this one. Essentially, Power BI isn't even attempting to calculate the measure.

     

    You could add a column and that should work but, again, you don't have any previous week sales in your data for the products that have blanks. So...

4 Replies

  • J94's avatar
    J94
    Advocate II

    Greg_Deckler thanks, I already thought it would be difficult to solve it with a measure. I will have a different approach and change the source instead. 

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    J94 Seems like it should be:

    Measure = 
        VAR __Week = MAX( 'Table (5)'[Week] )
        VAR __Product = MAX( 'Table (5)'[Product_bk] )
        VAR __Sales = MAX( 'Table (5)'[Sales] )
        VAR __PreviousWeek = 
            MAXX(
                FILTER( 
                    ALL( 'Table (5)' ),
                    [Product_bk] = __Product && [Sales] <> BLANK() 
                ),
                [Week]
            )
        VAR __Previous = MAXX( FILTER( ALL( 'Table (5)' ), [Product_bk] = __Product && [Week] = __PreviousWeek ), [Sales] )
        VAR __Result = IF( __Sales = BLANK(), __Previous, __Sales )
    RETURN
        __Result

     

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion

        J94 Well, yeah, because in your data you don't have any previous sales for the products that are blank. You are basically running into an Auto Exist issue. Essentially you are attempting to invent data that doesn't exist. The measure is not going to calculate on rows it feels are invalid. They were supposed to have fixed the auto exist issue with allowing you to select "Independent" as the "Value filter behavior" for the semantic model but it doesn't work in all cases, like this one. Essentially, Power BI isn't even attempting to calculate the measure.

         

        You could add a column and that should work but, again, you don't have any previous week sales in your data for the products that have blanks. So...