excelpowerquerym

Translate Excel Formula into Power Query


I would like to add a column with "Account No" in the General Ledger extracted from Yardi. Please see an example screenshot below:

Screenshot GL

Essentially, I need to fill in the column "Account No" with the values in column A that start with "11" for each row that has data. In the example screenshot, this would be only in rows 19 and 20.

I accomplished this with an Excel formula in column "L":

=IF(ISBLANK(B19);"";IF(ISNUMBER(LEFT(L18;1)*1);L18;A18))

I would need to translate this formula (or equivalent) into a Power Query custom column, but I have not idea how to do that.

Can you please help me with this?


Solution

  • Since you chose to provide only a screen shot, and not text which could be easily copy/pasted, my solution uses only the relevant columns.

    You will need to adapt the code to include your other columns.

    Algorithm:

    Source Data:
    enter image description here

    M-Code from Advanced Editor
    Edit: Code altered to test that Property starts with any digit

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Property", type text}, {"Property Name", type text}, {"Account No", type any}}),
    
        x = Text.StartsWith("1!","1"),
        
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Property"}, {
            {"AcctNum", (t)=>Table.ReplaceValue(
                t,
                each [Property Name],
                t[Property]{0},
                (x,y,z)=> if y=null then null
                            else if x <>null then x
                            else z,
                {"Account No"}), 
                type table [Property=nullable text, Property Name=nullable text, Account No=nullable text]}},
             GroupKind.Local,(x,y)=>Number.From(
                 List.Contains({"0".."9"}, Text.Start(x[Property],1)) and 
                 List.Contains({"0".."9"}, Text.Start(y[Property],1)))),
        
        
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Property"}),
        
        #"Expanded AcctNum" = Table.ExpandTableColumn(#"Removed Columns", "AcctNum", {"Property","Property Name", "Account No"})
    in 
        #"Expanded AcctNum"
    

    Result:
    enter image description here