Forum Discussion

FrankZappasMama's avatar
FrankZappasMama
Frequent Visitor
8 years ago
Solved

Calculations in Custom Column with LOOKUP and IF

Folks,   I'm struggling to create a custom column to the Project List dataset (explained below) that needs to lookup two values in the Project Cost dataset and return it. There are three relevant d...
  • erik_tarnvik's avatar
    erik_tarnvik
    8 years ago

    Another way, using the same sample data, is to use the TOPN function:

    ProjectCost2 = 
    CALCULATE(MAX(Costs[Cost]),
              TOPN(1,
                   FILTER(Costs,Costs[Category] = Projects[Category] && 
                                Costs[Date] <= Projects[Start]),
                   Costs[Date]))
  • erik_tarnvik's avatar
    erik_tarnvik
    8 years ago

    Of course. Bare with me though as this may appear a little more complex. I could post just the "M" code (you can see it below) but I assume it to be more helpful to lead you through the steps in the GUI to get there. Apply these steps:

    1. Use Get Data to load your Projects and Costs tables and edit in Power Query. Select the Projects table.

    2. Select Merge Queries. Select the Category table from Projects, select the Costs table in the dropdown, select the Category column in the Costs table and select an Inner Join in the drop down. Click OK.

    3. Expand the resulting tables in the Costs column in the result by clicking the icon to the right in the Costs column header.

    4. Now we need to only keep rows where Costs.Date is equal or earlier than Start date. Create a new custom column named Keep defined as [Costs.Date] <= [Start]. Click the filter button in the Keep header and unselect FALSE.

    5. Click Group By under transform. Click Advanced. Select ID as your Group By column. Enter CostDate as your New column name, select Max as you operation and select Costs.Date as Column. Click Add aggregation to get a new row. Enter Start, Max, Start for the fields. Click Add aggregation again. Enter Category, Max, Category. Click Add aggregation again. Enter ProjectCost, MAX, Costs.Cost. Repeat this as necessary if you have other columns you need to have in your result. Click OK. 

     

    Done. You can now remove/reorder/rename columns as appropriate. If you click Advanced Editor you can see the resulting "M" code, it should look something like this:

    let
        Source = Excel.Workbook(File.Contents("D:\OneDrive\file.xlsx"), null, true),
        Projects_Sheet = Source{[Item="Projects",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Projects_Sheet, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type text}, {"Start", type date}, {"Category", type text}, {"Sum", Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Category"},Costs,{"Category"},"Costs",JoinKind.Inner),
        #"Expanded Costs" = Table.ExpandTableColumn(#"Merged Queries", "Costs", {"Category", "Date", "Cost"}, {"Costs.Category", "Costs.Date", "Costs.Cost"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Costs", "Keep", each [Costs.Date] <= [Start]),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Keep] = true)),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ID"}, {{"CostDate", each List.Max([Costs.Date]), type date}, {"Start", each List.Max([Start]), type date}, {"Category", each List.Max([Category]), type text}, {"ProjectCost", each List.Max([Costs.Cost]), type number}})
    in
        #"Grouped Rows"

    Good luck, let me know if you run into issues.