Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Lookup Formula in Power Query

I have a monthly data consolidated in a single table, I need a Lookup formula for bringing previous month values to the current selected month, Is it Possible in Power Query? Also I cannot create ano...
  • ronrsnfld's avatar
    ronrsnfld
    2 years ago

    If I understand you correctly, paste the code below into the Advanced Editor. It seems to work with your data sample.

     

     

    Read the code and comments to better understand the algorithm.

     

     

    let
    
    //change next line to reflect actual data source
        Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parse", type date}}),
    
    //add index column to retain original order
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    
    //group by "Emp"
        #"Grouped Rows" = Table.Group(#"Added Index", {"Emp"}, {
    
        //For each emp
            {"all", (t)=>
                let 
    
                //ensure date is sorted ascending
                    sort = Table.Sort(t,{"Parse",Order.Ascending}),
                
                //shift segment down and up to compare this month to last month and next month
                    shift = Table.FromColumns(
                        Table.ToColumns(sort) & 
                        {{null} & List.RemoveLastN(t[Code Segment])} & 
                        {List.RemoveFirstN(t[Code Segment]) & {null}},
                       {"Parse","Emp","Code Segment","Index", "Shift Seg Down", "Shift Seg Up"}),
    
                    #"Add SegName" = Table.AddColumn(shift,"Segment Name",
                        each if [Code Segment] = ([Shift Seg Down]??[Code Segment]) then null else [Shift Seg Down]),
    
                    #"Add Xfr Cnt" = Table.AddColumn(#"Add SegName","Transfer Count",
                        each if [Code Segment] <> ([Shift Seg Up]??[Code Segment])
                            then -1
                            else if [Code Segment] = ([Shift Seg Down]??[Code Segment]) 
                            then 0 else 1),
    
                    #"Remove Shifted" = Table.RemoveColumns(#"Add Xfr Cnt",{"Shift Seg Down","Shift Seg Up"})
                in 
                    #"Remove Shifted",
                    type table[Parse=date, Emp=text, Code Segment=text, Index=Int64.Type,Segment Name=text, Transfer Count=Int64.Type]
                    }}),
    
    //Expand and sort the Grouped Columns
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Emp"}),
        #"Expanded all" = Table.ExpandTableColumn(#"Removed Columns", "all", {"Parse", "Emp", "Code Segment", "Index", "Segment Name", "Transfer Count"}),
        #"Sorted Rows" = Table.Sort(#"Expanded all",{{"Index", Order.Ascending}}),
        #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
    in
        #"Removed Columns1"