Forum Discussion

vpatel55's avatar
vpatel55
Icon for Kudo Commander rankKudo Commander
4 years ago
Solved

Converting from months to days in Power Query for every category

We have a monthly budget which I'm looking to convert to a daily value budget. This is by dividing the monthly budget by the days in the month. It needs to be done in Power Query (not DAX).

 

However I need this for every category, and for the model to repeat this, should a new category be added.

 

Kudos and huge thanks available to anyone who can help. Here is a file with the sample data.

 

https://www.dropbox.com/s/q7x4r76x1mculp8/Sample.pbix?dl=0

 

Here is the input:

 

MonthFruitCount
01/April/2022Apples819
01/May/2022Apples601
01/June/2022Apples423
01/April/2022Pears819
01/May/2022Pears601
01/June/2022Pears423
01/April/2022Oranges819
01/May/2022Oranges601
01/June/2022Oranges423

 

This is the expected output for Apples, but the same output would include Pears and Oranges, and any other category that may be added later on. Its dividing the monthly count for that category by the number of days in the month:

 

DateFruitCount
01/April/2022Apples27.3
02/April/2022Apples27.3
03/April/2022Apples27.3
04/April/2022Apples27.3
05/April/2022Apples27.3
…  
01/May/2022Apples19.3871
02/May/2022Apples19.3871
03/May/2022Apples19.3871
04/May/2022Apples19.3871
05/May/2022Apples19.3871
…  
01/June/2022Apples14.1
02/June/2022Apples14.1
03/June/2022Apples14.1
04/June/2022Apples14.1
05/June/2022Apples14.1

 

 

  • Hi vpatel55 ,

     

    Try this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUNzDRNzIwMlLSUXIsKMhJLQYyLAwtlWJ1ILKmGLJmBoZwWTMMWRMjY7gs3OSA1MQinAbDJLGaC5PEaqx/UWJeOm4XI6SxGo2QBhseCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Fruit = _t, Count = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Fruit", type text}, {"Count", Int64.Type}}),
        #"Added DailyCount" = Table.AddColumn(#"Changed Type", "DailyCount", each [Count] / Date.DaysInMonth([Month])),
        #"Added DaysMonth" = Table.AddColumn(#"Added DailyCount", "DaysMonth", each List.Dates([Month], Date.DaysInMonth([Month]), #duration(1,0,0,0))),
        #"Expanded DaysMonth" = Table.ExpandListColumn(#"Added DaysMonth", "DaysMonth")
    in
        #"Expanded DaysMonth"

4 Replies

  • latimeria's avatar
    latimeria
    Icon for Solution Specialist rankSolution Specialist

    Hi vpatel55 ,

     

    Try this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUNzDRNzIwMlLSUXIsKMhJLQYyLAwtlWJ1ILKmGLJmBoZwWTMMWRMjY7gs3OSA1MQinAbDJLGaC5PEaqx/UWJeOm4XI6SxGo2QBhseCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Fruit = _t, Count = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Fruit", type text}, {"Count", Int64.Type}}),
        #"Added DailyCount" = Table.AddColumn(#"Changed Type", "DailyCount", each [Count] / Date.DaysInMonth([Month])),
        #"Added DaysMonth" = Table.AddColumn(#"Added DailyCount", "DaysMonth", each List.Dates([Month], Date.DaysInMonth([Month]), #duration(1,0,0,0))),
        #"Expanded DaysMonth" = Table.ExpandListColumn(#"Added DaysMonth", "DaysMonth")
    in
        #"Expanded DaysMonth"
    • vpatel55's avatar
      vpatel55
      Icon for Kudo Commander rankKudo Commander

      latimeria you're a SUPERSTAR! Thanks so much, that works great. Now to study how it works! Many thanks and much appreciated 😄

    • vpatel55's avatar
      vpatel55
      Icon for Kudo Commander rankKudo Commander

      Hello ImkeF thanks so much for the response. I'm afraid my knowledge of Power Query is weak (I tried for hours before posting on here). Your fucntion looks useful but probably quite difficult for me to implement. Please would you be able to point me in the right direction on the sample file?  Thank you.