Forum Discussion

DekkerNick's avatar
DekkerNick
Frequent Visitor
2 years ago
Solved

Transform data in grouped periods

Hi all,  I have the following data example:  As shown it has periods of consecutive dates which I want to group for the PowerBI Gantt Chart. So that in the Gantt Chart the grouped dates a...
  • lbendlin's avatar
    lbendlin
    2 years ago
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdY9a8MwEAbgv2I8x2B9S2vbqVOgkAwhg0tFCRgT7P5/eunQYl2Hs14PBiP8vDrsQ77LpX0a5q/mmO95XNpDez41pzy/36aP2/SZJ1pp6NKx853utaX7t/PLY8W314MQJwCbHsCqC4UN8qoBawBroZrj2hq1oeZ6awHrmNVi6wEbABuhmlNh5f0cABsBm5iV96TqEawQrLGdqfKVtvKmpq0RbSBtuZa3p3KQ9pAOaOWq0Bt+FR7SAdKR6w2dmhCte0grSNNb02vt5N+bZhJEJ0TTVMK0kWuFaMIP636t/GjQ/9PnnJfmdZiWnwcL2hyHeRjHPP4dL+VAZitS3C4pfpeUwFJcRQofkWtS+KxckaL50FxXSzmKpqpa9kjhozylXL8B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Naam = _t, Groep = _t, Aanwijzing = _t, Datum = _t, Waarde = _t, SumWeeknummer = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Datum", type date}},"nl"),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Naam", "Groep", "Aanwijzing", "Waarde", "Datum", "Index"}, {{"Rows", each _, type table [Naam=nullable text, Groep=nullable text, Aanwijzing=nullable text, Datum=nullable date, Waarde=nullable text, SumWeeknummer=nullable text, Index=number]}},GroupKind.Local,(x,y)=>Number.From(try y[Datum]<>Date.AddDays(#"Added Index"{y[Index]-1}[Datum],1) otherwise true))
    in
        #"Grouped Rows"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

     

    Obligatory reference: ImkeF https://www.thebiccountant.com/2018/01/21/table-group-exploring-the-5th-element-in-power-bi-and-power-query

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi DekkerNick 

     

    Here is my method for your reference.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdY9a8MwEAbgv2I8x2B9S2vbqVOgkAwhg0tFCRgT7P5/eunQYl2Hs14PBiP8vDrsQ77LpX0a5q/mmO95XNpDez41pzy/36aP2/SZJ1pp6NKx853utaX7t/PLY8W314MQJwCbHsCqC4UN8qoBawBroZrj2hq1oeZ6awHrmNVi6wEbABuhmlNh5f0cABsBm5iV96TqEawQrLGdqfKVtvKmpq0RbSBtuZa3p3KQ9pAOaOWq0Bt+FR7SAdKR6w2dmhCte0grSNNb02vt5N+bZhJEJ0TTVMK0kWuFaMIP636t/GjQ/9PnnJfmdZiWnwcL2hyHeRjHPP4dL+VAZitS3C4pfpeUwFJcRQofkWtS+KxckaL50FxXSzmKpqpa9kjhozylXL8B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Naam = _t, Groep = _t, Aanwijzing = _t, Datum = _t, Waarde = _t, SumWeeknummer = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Naam", type text}, {"Groep", type text}, {"Aanwijzing", type text}, {"Datum", type date}, {"Waarde", type text}, {"SumWeeknummer", Int64.Type}}, "en-GB"),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Naam", Order.Ascending}, {"Groep", Order.Ascending}, {"Datum", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
        #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
        #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Naam", "Groep", "Aanwijzing", "Waarde", "Index"}, #"Added Index1", {"Naam", "Groep", "Aanwijzing", "Waarde", "Index.1"}, "Added Index1", JoinKind.LeftOuter),
        #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"Datum"}, {"Datum.1"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Added Index1", "Custom", each if [Datum.1] = Date.AddDays([Datum],1) then 1 else 0),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "End Date", each if [Custom] = 0 then [Datum] else null),
        #"Filled Up" = Table.FillUp(#"Added Custom1",{"End Date"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Filled Up",{{"End Date", type date}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"Naam", "Groep", "Waarde", "Aanwijzing", "End Date"}, {{"Start Date", each List.Min([Datum]), type nullable date}})
    in
        #"Grouped Rows"

     

    Best Regards,
    Jing
    If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!