Forum Discussion

lukaszibula's avatar
lukaszibula
New Member
5 years ago
Solved

Counter for one unique expression for every row

Hello, I try to get a counter for an unique expression in my table. My table looks something like this:   voteOptionText    voteAnswer Date                     2021.03.04 Company             Tes...
  • mahoneypat's avatar
    5 years ago

    Here is one way to do it in the query editor.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  Note that the Rename and Trim steps were only needed as the data you pasted had leading/trailing spaces.

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckksST20ADtU0lFCF1IwMjAy1DMw1jMwUYrViVZyzs8tSMyrxKNXISS1uCQZogysxTOvuKSoNDc1r0SjWBNdMULSkBTFRjDHgDjFONwCdolCMkQRWAPZnjcjw/NGNPY9kYqNiQqq/JKM1CJEWMUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"voteOptionText  " = _t, #"  voteAnswer" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"voteOptionText  ", type text}, {"  voteAnswer", type text}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"voteOptionText  ", "voteOptionText"}, {"  voteAnswer", "voteAnswer"}}),
        #"Trimmed Text" = Table.TransformColumns(#"Renamed Columns",{{"voteOptionText", Text.Trim, type text}, {"voteAnswer", Text.Trim, type text}}),
        #"Added Custom" = Table.AddColumn(#"Trimmed Text", "DateForFillDown", each if [voteOptionText] = "Date" then [voteAnswer] else null, type text),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"DateForFillDown"}),
        #"Grouped Rows" = Table.Group(#"Filled Down", {"DateForFillDown"}, {{"AllRows", each _, type table [voteOptionText=text, voteAnswer=text, DateForFillDown=text]}}),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Added Index", "AllRows", {"voteOptionText", "voteAnswer"}, {"voteOptionText", "voteAnswer"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded AllRows",{"DateForFillDown"})
    in
        #"Removed Columns"

     

     

    Pat