Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Indexing using PowerQuery

Good day,

 

Please could someone tell me if it is possible to Index columns.  I need two columns, one Index column starting at 0 and the other starting at 1.  The only issue is it needs to be grouped by Client no.

 

Please see below example.

ClientNoDateIce CreamsRateIndexIndex From 0
12342019/12/01200001010
12342019/12/0110001021
12342019/12/0150001132
12342019/12/0220001243
12342019/12/0290001154
12342019/12/0250001365
12342019/12/0360001476
12342019/12/0460001287
12342019/12/0550001198
12582019/12/0520002010
12582019/12/0510001021
12582019/12/0560001532
12582019/12/0550001243
12582019/12/0610001354
12582019/12/0640001465
12582019/12/0650001576
12582019/12/0660002087
12582019/12/0640001098
12582019/12/0650011109

 

Thanks in advance !

 

Regards,

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Anonymous,

you have to Group the data and then apply TransformColumns twice

Here the solution for you

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdBRDsAQDAbgu3iW0GplziLuf43Zwma040UkX/x/m7MBDGSsQQ/JAToP96OeeoM3xcoEtoKbAFFgi7kEqiJt/+gpQRShPmITJAoahNyDhVn4mEWbBb0q5o2tovdgVfDU9CvikBJUQdM+VsG/PeLbVJl2TNEFPystJw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ClientNo = _t, Date = _t, #"Ice Creams" = _t, Rate = _t]),
    #"Changed Type" = Table.TransformColumnTypes
    (
        Source,
        {{"ClientNo", Int64.Type}, {"Date", type date}, {"Ice Creams", Int64.Type}, {"Rate", Int64.Type}}
    ),
    #"Grouped Rows" = Table.Group
    (
        #"Changed Type", 
        {"ClientNo"}, 
        {{"AllRows", each _, type table [ClientNo=number, Date=date, Ice Creams=number, Rate=number]}}
    ),
    AddIndex0 = Table.TransformColumns
    (
        #"Grouped Rows",
        {{"AllRows", each Table.AddIndexColumn
        (
            _,
            "Index0",
            0
        )}}
    ),
    AddIndex1 = Table.TransformColumns
    (
        AddIndex0,
        {{"AllRows", each Table.AddIndexColumn
        (
            _,
            "Index1",
            1
        )}}
    ),
    #"Expanded AllRows" = Table.ExpandTableColumn(AddIndex1, "AllRows", {"Date", "Ice Creams", "Rate", "Index0", "Index1"}, {"Date", "Ice Creams", "Rate", "Index0", "Index1"})

in
    #"Expanded AllRows"

 

If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun

Jimmy

View solution in original post

3 REPLIES 3
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

any news?

 

Jimmy

Jimmy801
Community Champion
Community Champion

Hello @Anonymous,

you have to Group the data and then apply TransformColumns twice

Here the solution for you

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdBRDsAQDAbgu3iW0GplziLuf43Zwma040UkX/x/m7MBDGSsQQ/JAToP96OeeoM3xcoEtoKbAFFgi7kEqiJt/+gpQRShPmITJAoahNyDhVn4mEWbBb0q5o2tovdgVfDU9CvikBJUQdM+VsG/PeLbVJl2TNEFPystJw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ClientNo = _t, Date = _t, #"Ice Creams" = _t, Rate = _t]),
    #"Changed Type" = Table.TransformColumnTypes
    (
        Source,
        {{"ClientNo", Int64.Type}, {"Date", type date}, {"Ice Creams", Int64.Type}, {"Rate", Int64.Type}}
    ),
    #"Grouped Rows" = Table.Group
    (
        #"Changed Type", 
        {"ClientNo"}, 
        {{"AllRows", each _, type table [ClientNo=number, Date=date, Ice Creams=number, Rate=number]}}
    ),
    AddIndex0 = Table.TransformColumns
    (
        #"Grouped Rows",
        {{"AllRows", each Table.AddIndexColumn
        (
            _,
            "Index0",
            0
        )}}
    ),
    AddIndex1 = Table.TransformColumns
    (
        AddIndex0,
        {{"AllRows", each Table.AddIndexColumn
        (
            _,
            "Index1",
            1
        )}}
    ),
    #"Expanded AllRows" = Table.ExpandTableColumn(AddIndex1, "AllRows", {"Date", "Ice Creams", "Rate", "Index0", "Index1"}, {"Date", "Ice Creams", "Rate", "Index0", "Index1"})

in
    #"Expanded AllRows"

 

If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun

Jimmy

stvsurf
Resolver I
Resolver I

First you want to do a "group by", with "All rows" as your operation, and you can give the column a name. e.g. "group"

Then add a custom column with the formula:

= Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([group],"Index"))

 

Next you want to expand the index column of the new table 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors