Forum Discussion

ANBILY1's avatar
ANBILY1
Frequent Visitor
1 year ago
Solved

Create Groups of Equal Size

Dear Sir, as per the example below, I need to group by 5 blank spaces in the value column in relation to the date and code column, that is, 5 consecutive dates from the most recent onwards that hav...
  • sanalytics's avatar
    1 year ago

    ANBILY1 
    Below is the correct code which dynamically group the code and based on that it splits the table.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("xdExDoAgEAXRu2yNCaAolC57C8L9ryEdGKe3oHkhcT62Jj5swW/RxyRO7nGku6ERdUc9UBPqiXqhZtRCOojurtsUtyluU9ymuE1/+tr6khVfsmKZYZlhmWGZYZlhmWHZ1IxaSF//eCpuC99t/QE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Code = _t, Value = _t]),
        TypeChanged = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Code", type text}, {"Value", type text}}),
        Result = Table.Combine( 
    let
    CodeGrouping =
    Table.Group(
    TypeChanged,{"Code"},{"AllRows", each _ }
    )[AllRows],
    SplitTable =
    List.Combine(
    List.Transform( 
    CodeGrouping,(x) =>
    Table.Split(x,5)
    ) ),
    Result =
    List.Transform( 
    SplitTable,(x) =>
    let
    RowCountAdded =Table.AddColumn(
        x,"RowCount",each Table.RowCount(x) ),
    ListOfDatesAdded = 
    Table.AddColumn(RowCountAdded,"Dates",each RowCountAdded[Date]),
    DateRemoved = 
    Table.SelectColumns(ListOfDatesAdded,
    List.Select(Table.ColumnNames(ListOfDatesAdded), each _ <>"Date") ),
    RemoveDuplicate = 
    Table.Distinct(DateRemoved)
    in 
    RemoveDuplicate
    )
    in 
    Result
    )
    in
        Result

     

    I have imported more 12 rows as Code D. It dynamically groups and split the table as 5 rows. Below screenshot

     

    one suggesation, please do not use List.FirstN if you are dealing with large data.. It technically slow  down the query performance.

     

    Attaching the file for your reference.

     

    Hope it helps

     

    Regards,

    sanalytics