Forum Discussion

JakubWiniarczk's avatar
JakubWiniarczk
Frequent Visitor
6 years ago
Solved

PowerQuery - fill value in column based on condition

Hi Everyone,

 

I've some problem with creating PowerQuery formula for my data like below:

IndeksDescriptionValue
1Opis1Test
2Opis2 
3Opis3 
4Opis4 
5Opis4Test2
6Opis5 
7Opis6 
8Opis7Test3
9Opis7 
10Opis8 
11Opis9Test4
12Opis10 

 

In my excel file, I've got filled column 'Value' only for the first row in the group. I need to create a new column for sheet-like below:

IndeksDescriptionValueNew Column
1Opis1TestTest
2Opis2 Test
3Opis3 Test
4Opis4 Test
5Opis4Test2Test2
6Opis5 Test2
7Opis6 Test2
8Opis7Test3Test3
9Opis7 Test3
10Opis8 Test3
11Opis9Test4Test4
12Opis10 

Test4

 

Row for each group maybe is different for each import.

Any ideas? 

 

  • Hello JakubWiniarczk 

     

    I've prepared a solution for you. This involves replacing empty cells with null and then apply a filldown.

    Here the code

    let
    	Source = #table
    	(
    		{"Indeks","Description","Value"},
    		{
    			{"1","Opis1","Test"},	{"2","Opis2",""},	{"3","Opis3",""},	{"4","Opis4",""},	{"5","Opis4","Test2"},	{"6","Opis5",""},	{"7","Opis6",""},	{"8","Opis7","Test3"},	
    			{"9","Opis7",""},	{"10","Opis8",""},	{"11","Opis9","Test4"},	{"12","Opis10",""}
    		}
    	),
        ReplaceEmptywithNull = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Value"}),
        FillDown = Table.FillDown(ReplaceEmptywithNull,{"Value"})
    in
    	FillDown

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • Hi JakubWiniarczk ,

     

    In power query, we can fill the column down as below.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc69CoAwDATgd8ncwTbp31O4uJWODm5CfX+k8SKdDj5y4VojT472+xozj3M81F2jAJypwAA2EIAYxAXmn6CaoNHOMiAZFEBGj1Xrogp+g5RfbHlFUz627dqg3l8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Indeks = _t, Description = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Indeks", Int64.Type}, {"Description", type text}, {"Value", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Value"}),
        #"Filled Down" = Table.FillDown(#"Replaced Value",{"Value"})
    in
        #"Filled Down"

     

    Alternatively, We can achieve that by DAX.

    Column = 
    VAR a = 'Table (2)'[Indeks]
    VAR ind =
        CALCULATE (
            MAX ( 'Table (2)'[Indeks] ),
            FILTER (
                'Table (2)',
                'Table (2)'[Value] <> BLANK ()
                    && 'Table (2)'[Indeks] <= a
            )
        )
    RETURN
        CALCULATE (
            MAX ( 'Table (2)'[Value] ),
            FILTER ( 'Table (2)', 'Table (2)'[Indeks] = ind )
        )
    

     

    Pbix as attached.

     

3 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello JakubWiniarczk 

     

    I've prepared a solution for you. This involves replacing empty cells with null and then apply a filldown.

    Here the code

    let
    	Source = #table
    	(
    		{"Indeks","Description","Value"},
    		{
    			{"1","Opis1","Test"},	{"2","Opis2",""},	{"3","Opis3",""},	{"4","Opis4",""},	{"5","Opis4","Test2"},	{"6","Opis5",""},	{"7","Opis6",""},	{"8","Opis7","Test3"},	
    			{"9","Opis7",""},	{"10","Opis8",""},	{"11","Opis9","Test4"},	{"12","Opis10",""}
    		}
    	),
        ReplaceEmptywithNull = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Value"}),
        FillDown = Table.FillDown(ReplaceEmptywithNull,{"Value"})
    in
    	FillDown

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • v-frfei-msft's avatar
    v-frfei-msft
    Community Support

    Hi JakubWiniarczk ,

     

    In power query, we can fill the column down as below.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc69CoAwDATgd8ncwTbp31O4uJWODm5CfX+k8SKdDj5y4VojT472+xozj3M81F2jAJypwAA2EIAYxAXmn6CaoNHOMiAZFEBGj1Xrogp+g5RfbHlFUz627dqg3l8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Indeks = _t, Description = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Indeks", Int64.Type}, {"Description", type text}, {"Value", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Value"}),
        #"Filled Down" = Table.FillDown(#"Replaced Value",{"Value"})
    in
        #"Filled Down"

     

    Alternatively, We can achieve that by DAX.

    Column = 
    VAR a = 'Table (2)'[Indeks]
    VAR ind =
        CALCULATE (
            MAX ( 'Table (2)'[Indeks] ),
            FILTER (
                'Table (2)',
                'Table (2)'[Value] <> BLANK ()
                    && 'Table (2)'[Indeks] <= a
            )
        )
    RETURN
        CALCULATE (
            MAX ( 'Table (2)'[Value] ),
            FILTER ( 'Table (2)', 'Table (2)'[Indeks] = ind )
        )
    

     

    Pbix as attached.

     

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello JakubWiniarczk 

    have you been able to solve the problem with the replies given?

    If so, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    All the best

    Jimmy