Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Dynamic variable in PQ to compare and calculate total

Hi guys,   is there a way to store/change a variable in a loop? I want to calculate the total amount of days products were available and only take the longst timeframe. (All those products are th...
  • _AlexandreRM_'s avatar
    3 years ago

    Hello Anonymous , here is an example of what you could do with PQ:

     

    let
    Source = Excel.CurrentWorkbook(){[Name="DatesDiff"]}[Content],
    typed = Table.TransformColumnTypes(Source,{{"Post date", type date}, {"Unpost date", type date}}),
    datesListsAdded = Table.AddColumn(typed, "Dates list", each List.Dates([Post date], Duration.Days([Unpost date] - [Post date]) + 1, #duration(1, 0, 0, 0))),
    datesListsExpanded = Table.ExpandListColumn(datesListsAdded, "Dates list"),
    count = List.Count(List.Distinct(datesListsExpanded[Dates list]))
    in
    count

     

    The global behavior is to generate a list of all dates between each post and unpost date, expand these lists, and count the number of distinct dates.

     

    With your sample data, I get 162 days, which is what I also find with manual calculation (you are missing some days in your own calculation, you should recheck it).

     

    Hope this helps.