Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Split current month values to daily

Hi,

 

I`m a newbie to Power BI. I`ve run into an issue & couldnt find the solution I`m looking for. 

 

After getting the data from the source, in power query I`d like to split the current month value to daily rows depending on the product type. Below is the data

 

ProductQtyPriceStart DateEnd Date
A50257/1/20207/31/2020
B100357/1/20207/31/2020
A75308/1/20208/31/2020
B30409/1/20209/30/2020
B853510/1/202010/31/2020

 

Expected Result - Assume today is 07/22/2020, the new column should have daily values starting from yesterday, 07/21/2020 for the current month rows & divide the quantity by number of days in the month for product A. Similarly product B is divided by number of working days in the current month.

 

Next month data rows will stay monthly & when the month becomes current, the logic will split to daily rows.

 

I used - List.Dates([START DATE],Date.Day(Date.EndOfMonth([START DATE])), #duration(1,0, 0, 0)), but this splits to daily rows for all the months in the data.

 

ProductQtyPriceStart DateEnd Date
A1.613257/21/20207/21/2020
A1.613257/22/20207/22/2020
A1.613257/23/20207/23/2020
A1.613257/24/20207/24/2020
A1.613257/25/20207/25/2020
A1.613257/26/20207/26/2020
A1.613257/27/20207/27/2020
A1.613257/28/20207/28/2020
A1.613257/29/20207/29/2020
A1.613257/30/20207/30/2020
A1.613257/31/20207/31/2020
B4.348357/21/20207/21/2020
B4.348357/22/20207/22/2020
B4.348357/23/20207/23/2020
B4.348357/24/20207/24/2020
B4.348357/25/20207/25/2020
B4.348357/26/20207/26/2020
B4.348357/27/20207/27/2020
B4.348357/28/20207/28/2020
B4.348357/29/20207/29/2020
B4.348357/30/20207/30/2020
B4.348357/31/20207/31/2020
A75308/1/20208/31/2020
B30409/1/20209/30/2020
B853510/1/202010/31/2020

 

I understand doing this in power query is performance efficient than a DAX column. Please suggest what`s best here.

 

Thanks in advance.

Anil

  • I am still curious what analysis/visual you plan to do, but this was a fun query challenge so I went ahead and did it (I think).  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

     

    let
      Source = Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText(
              "i45WclTSUTI1ABJGpkDCXN9Q38jAyADMNIayY3WilZyAIoYGIHFjvOpAxpmDVBiD5CwQyiwwjAOrMAERlghllvrGBqjKLExhlhoaINQB2XDzYgE=", 
              BinaryEncoding.Base64
            ), 
            Compression.Deflate
          )
        ), 
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table[Product = _t, Qty = _t, Price = _t, #"Start Date" = _t, #"End Date" = _t]
      ),
      #"Changed Type" = Table.TransformColumnTypes(
        Source, 
        {
          {"Product", type text}, 
          {"Qty", Int64.Type}, 
          {"Price", Int64.Type}, 
          {"Start Date", type date}, 
          {"End Date", type date}
        }
      ),
      #"Added Custom" = Table.AddColumn(
        #"Changed Type", 
        "New Start Date", 
        each 
          let
            thisdate = Date.From(DateTime.LocalNow())
          in
              if Date.Month([Start Date]) = Date.Month(thisdate)
              then List.Dates(
                Date.AddDays(thisdate, - 1), 
                Duration.TotalDays(Date.EndOfMonth(thisdate) - thisdate) + 1, 
                #duration(1, 0, 0, 0)
              )
              else {[Start Date]}
      ),
      #"Added Custom1" = Table.AddColumn(
        #"Added Custom", 
        "New Qty", 
        each [Qty] / List.Count([New Start Date])
      ),
      #"Expanded DateList" = Table.ExpandListColumn(#"Added Custom1", "New Start Date"),
      #"Changed Type1" = Table.TransformColumnTypes(
        #"Expanded DateList", 
        {{"New Start Date", type date}, {"New Qty", type number}}
      )
    in
      #"Changed Type1"

     

    I didn't add the end date, but that should be straight forward if needed.  The above does what you were looking for expanding the current month to daily level and calculating the daily quantity.

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

3 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    I am still curious what analysis/visual you plan to do, but this was a fun query challenge so I went ahead and did it (I think).  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

     

    let
      Source = Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText(
              "i45WclTSUTI1ABJGpkDCXN9Q38jAyADMNIayY3WilZyAIoYGIHFjvOpAxpmDVBiD5CwQyiwwjAOrMAERlghllvrGBqjKLExhlhoaINQB2XDzYgE=", 
              BinaryEncoding.Base64
            ), 
            Compression.Deflate
          )
        ), 
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table[Product = _t, Qty = _t, Price = _t, #"Start Date" = _t, #"End Date" = _t]
      ),
      #"Changed Type" = Table.TransformColumnTypes(
        Source, 
        {
          {"Product", type text}, 
          {"Qty", Int64.Type}, 
          {"Price", Int64.Type}, 
          {"Start Date", type date}, 
          {"End Date", type date}
        }
      ),
      #"Added Custom" = Table.AddColumn(
        #"Changed Type", 
        "New Start Date", 
        each 
          let
            thisdate = Date.From(DateTime.LocalNow())
          in
              if Date.Month([Start Date]) = Date.Month(thisdate)
              then List.Dates(
                Date.AddDays(thisdate, - 1), 
                Duration.TotalDays(Date.EndOfMonth(thisdate) - thisdate) + 1, 
                #duration(1, 0, 0, 0)
              )
              else {[Start Date]}
      ),
      #"Added Custom1" = Table.AddColumn(
        #"Added Custom", 
        "New Qty", 
        each [Qty] / List.Count([New Start Date])
      ),
      #"Expanded DateList" = Table.ExpandListColumn(#"Added Custom1", "New Start Date"),
      #"Changed Type1" = Table.TransformColumnTypes(
        #"Expanded DateList", 
        {{"New Start Date", type date}, {"New Qty", type number}}
      )
    in
      #"Changed Type1"

     

    I didn't add the end date, but that should be straight forward if needed.  The above does what you were looking for expanding the current month to daily level and calculating the daily quantity.

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    This is doable in the query editor, but can I ask what calculation/visualization you plan to do once the data are in that format?  There may be a way to get your result directly with DAX from your data in monthly form.

     

    Regards,

    Pat

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      mahoneypat - Thanks for your response. It worked liked a charm.

       

      As I was exploring, I came across the IsInCurrentMonth function & used the below. I expanded the list & filtered on dates. it worked as expected

       

      if Date.IsInCurrentMonth([START_DATE]) then List.Dates([START_DATE],Date.Day(Date.EndOfMonth([START_DATE])), #duration(1,0, 0, 0)) else null

       

      The calculation on the quantity was acheieved using another column in the data.

       

      Kudos for your help.

       

      Cheers,

      Anil