Forum Discussion

Ricardo_boy_ser's avatar
Ricardo_boy_ser
Regular Visitor
5 years ago
Solved

Power Query - create a column with values based on comparing two tables dates

Hi, I need to create a calculated column in which every row can have a different value but every one has the same logical operation.   I have two tables: TABLE-A has the data info which contains ...
  • edhans's avatar
    5 years ago

    Ok Ricardo_boy_ser see if this works. This is my result - I filtered out the 0's for this screenshot so you could see your data matched mine - the last two columns:

    Now, I want to explain why I did it the way I did. On Planet Excel you'd just do a vlookup like you have, and all is good. Power Query doesn't work that way though. It does everything row by row. But the Excel way might look like this:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"ITEM", type text}, {"POS RETAIL", Currency.Type}, {"MTD", Currency.Type}, {"YTD", Currency.Type}}),
        YearMatch =
            Table.AddColumn(
                #"Changed Type",
                "Year Match",
                each 
                    let
                        varLookupDate = [DATE]
                    in
                        Table.RowCount(
                            Table.SelectRows(
                                Calendar,
                                each [YTD FROM] < varLookupDate and [YTD TO] > varLookupDate
                            )
                    )
            ),
        #"Filtered Rows" = Table.SelectRows(YearMatch, each ([Year Match] = 1))
    in
        #"Filtered Rows"

    That will return a 1 or 0 depending on whether or not the date is in the date range desired. But when i went to load that, it got to 5,000 records very quickly, then started to drag, and I cancelled it. I knew that would be the result. You can still see this in the file below in the Data (2) table. But don't use that logic.

     

    What I did was this:

    Your Calendar table had start/end ranges. I converted those to a list of numbers, expanded them, then to dates using this code:

    let
        Source = Calendar,
        #"Added Year Range" = Table.AddColumn(Source, "Year Range", each {Number.From([YTD FROM])..Number.From([YTD TO])}),
        #"Expanded Year Range" = Table.ExpandListColumn(#"Added Year Range", "Year Range"),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Year Range",{{"Year Range", type date}})
    in
        #"Changed Type"

    It returned this table:

    I then merged that column with the Date column in your Data table. If there is a match, pull the POS, if not, return 0. That is this code in the Data table, which starts with your Excel file.

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"ITEM", type text}, {"POS RETAIL", Currency.Type}, {"MTD", Currency.Type}, {"YTD", Currency.Type}}),
        #"Merged Calendar Year Range" = Table.NestedJoin(#"Changed Type", {"DATE"}, #"Calendar Year Range", {"Year Range"}, "Calendar Year Range", JoinKind.LeftOuter),
        #"Expanded Calendar Year Range" = Table.ExpandTableColumn(#"Merged Calendar Year Range", "Calendar Year Range", {"YEAR"}, {"YEAR"}),
        #"Added New Year POS Retail" = Table.AddColumn(#"Expanded Calendar Year Range", "New Year POS Retail", each if [YEAR] = null then 0 else [POS RETAIL], Currency.Type),
        #"Merged Calendar Month Range" = Table.NestedJoin(#"Added New Year POS Retail", {"DATE"}, #"Calendar Month Range", {"Month Range"}, "Calendar Month Range", JoinKind.LeftOuter),
        #"Expanded Calendar Month Range" = Table.ExpandTableColumn(#"Merged Calendar Month Range", "Calendar Month Range", {"YEAR"}, {"YEAR.1"}),
        #"Added New Month POS Retail" = Table.AddColumn(#"Expanded Calendar Month Range", "New Month POS Retail", each if [YEAR.1] is null then 0 else [POS RETAIL], Currency.Type),
        #"Removed Other Columns" = Table.SelectColumns(#"Added New Month POS Retail",{"DATE", "ITEM", "YTD", "MTD", "New Year POS Retail", "New Month POS Retail"})
    in
        #"Removed Other Columns"

    Then I did it again for the Month ranges.

     

    This returned the desired results for all 270K records as fast as Excel could fill the cells.

     

    Here is your Excel file back with all queries. Nothing I did is "loaded" because it would just bloat the file size, but you can load the Data table and see the true results. Same thing as if you loaded it to Power BI's Data model. But leave the two calendar tables I created as NOT LOADED - they are just used for processing.

     

    Your Excel file.