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
rajrajsha
Frequent Visitor

Increment the cell value by 1 based on the IF condition criteria

Hi, All

Im new to this chat.

Im looking for one of the solution in power query. can you team suggest me the solution.

I tried the way in PQ and i got stuck in incrementing the cell based on criteria.

below image is the solution in need. (colulmn E is the solution i need in PQ)

 

image.png

Below is the excel formal in E column, that im trying to implement in PQ.

image.png

 

IDAllot #In orderOut order0
264641910/18/2017 11:0110/18/2017 14:231
356201011/7/2016 16:1812/8/2016 9:352
355229111/8/2016 8:5712/5/2016 14:322
35360911/14/2016 9:0711/14/2016 10:002
351150012/22/2016 12:531/3/2017 12:553
352407012/27/2016 7:5412/27/2016 8:303
357000712/27/2016 10:361/11/2017 14:444
35231581/11/2017 7:531/11/2017 9:154
35979071/23/2017 7:352/3/2017 12:465
35979411/23/2017 10:202/20/2017 14:435
35188601/30/2017 10:231/31/2017 6:495
35781482/1/2017 11:362/1/2017 12:106
3529372/2/2017 16:212/17/2017 8:277
35669305/1/2017 7:535/23/2017 21:108
35863295/4/2017 11:115/4/2017 12:488
35988556/27/2017 8:398/1/2017 10:179
35434897/6/2017 10:237/6/2017 10:599
35434437/6/2017 10:237/6/2017 11:049
35250187/6/2017 11:068/8/2017 11:4410
35436368/24/2017 9:1110/24/2017 8:4811
3558639/6/2017 8:569/6/2017 11:2111
35388659/28/2017 7:479/28/2017 9:2012
35574909/28/2017 7:599/28/2017 8:4012
399687110/6/2016 15:411/3/2017 10:1513
397150710/12/2016 10:2410/12/2016 12:1913
398138610/31/2016 11:171/5/2017 12:0714
396952111/22/2016 9:1212/19/2016 13:1014
39296112/8/2016 9:0412/8/2016 11:0314
434749893/3/2017 13:113/21/2017 12:4015
437125493/9/2017 7:514/12/2017 15:4415
439554543/14/2017 9:383/14/2017 11:2115

 

 I have added a sample of date for your convinence. Thanks for you reply.

 

Please ask any question if not able to understand my explination.

Thanks

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @rajrajsha 

Check my queries and apply on your side.

If you have any problem, feel free to let me know.

Capture8.JPG

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZRdkuwgCIW3MtXPUxX5Fd1K1+x/GxfBaMx9mslpPsEj8P1+UD+/H1aG5n+hXGAXFqg/AL3AS+KONKTP3+/3Q+L/qv9QhgRXHUH6A9rBhoKXpdJ6hOKmBLFBUjPGutSEZB7DnfCkSEtLCPg+udRTgdJLOTEACcnPRpxR2CUuctG8mQuRYmPIpd7YvFrtwqdincqJ1VJKPYO8JtLI5oXeRvI4iR/pCMSOoHrXeAutg5xQqy2TXUg3lKc9LsYjtxwQwwF5gRim+ecukE4MzLSkZ2Vj08VZoXZuJ1UN2OJoWG0VZmwBe3SQPrxoVLOcGaIdIZmainUcEfXRiNriKeQ67ZN1S4RMZBsyJYyCL17VARyC+2cn1MyiV3Q+8KiGxim2rlQ6jPLahpjYRky99DTvKUj7D+J3zAvyIeUTQikxf0eMRnl7sqP73I1HKqUMQl7dNsf/ViytgMf4izs40t+5fIz1+e2pEF4MeSOFjxfa/VRcD6FlP8JzZVRu5UWlX0vw+p5U/KhW5y10DqP02f27+WOsgDZWfWXUufvuleHW80vyzm0v0oBMMyyHQqOnckhl9VSezpvTJtOotaT8ATAXCbR5EmUHP0FsCq9tW/gQxvvTpqKj2N2MfqRlA+V7++cezPRTNlcBhZNr6xUGxtOVGgbzC2siHKuT5qIeT0x2CKtVnPv7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, #"Allot #" = _t, #"In order" = _t, #"Out order" = _t, #"0" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Allot #", Int64.Type}, {"In order", type datetime}, {"Out order", type datetime}, {"0", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 2, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index1", {"Index.1"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"ID", "Out order"}, {"Added Index1.ID", "Added Index1.Out order"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Added Index1",{{"Added Index1.ID", "previous.ID"}, {"Added Index1.Out order", "previous.Out order"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Index.1"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each if [ID]=[previous.ID] and  [In order]<=[previous.Out order] then 0 else 1),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}}),
    #"Added Custom1" = Table.AddColumn(#"Sorted Rows", "Custom.1", each List.Sum(List.FirstN(#"Sorted Rows"[Custom],[Index])))
in
    #"Added Custom1"

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-juanli-msft
Community Support
Community Support

Hi @rajrajsha 

Check my queries and apply on your side.

If you have any problem, feel free to let me know.

Capture8.JPG

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZZRdkuwgCIW3MtXPUxX5Fd1K1+x/GxfBaMx9mslpPsEj8P1+UD+/H1aG5n+hXGAXFqg/AL3AS+KONKTP3+/3Q+L/qv9QhgRXHUH6A9rBhoKXpdJ6hOKmBLFBUjPGutSEZB7DnfCkSEtLCPg+udRTgdJLOTEACcnPRpxR2CUuctG8mQuRYmPIpd7YvFrtwqdincqJ1VJKPYO8JtLI5oXeRvI4iR/pCMSOoHrXeAutg5xQqy2TXUg3lKc9LsYjtxwQwwF5gRim+ecukE4MzLSkZ2Vj08VZoXZuJ1UN2OJoWG0VZmwBe3SQPrxoVLOcGaIdIZmainUcEfXRiNriKeQ67ZN1S4RMZBsyJYyCL17VARyC+2cn1MyiV3Q+8KiGxim2rlQ6jPLahpjYRky99DTvKUj7D+J3zAvyIeUTQikxf0eMRnl7sqP73I1HKqUMQl7dNsf/ViytgMf4izs40t+5fIz1+e2pEF4MeSOFjxfa/VRcD6FlP8JzZVRu5UWlX0vw+p5U/KhW5y10DqP02f27+WOsgDZWfWXUufvuleHW80vyzm0v0oBMMyyHQqOnckhl9VSezpvTJtOotaT8ATAXCbR5EmUHP0FsCq9tW/gQxvvTpqKj2N2MfqRlA+V7++cezPRTNlcBhZNr6xUGxtOVGgbzC2siHKuT5qIeT0x2CKtVnPv7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, #"Allot #" = _t, #"In order" = _t, #"Out order" = _t, #"0" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Allot #", Int64.Type}, {"In order", type datetime}, {"Out order", type datetime}, {"0", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 2, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index1", {"Index.1"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"ID", "Out order"}, {"Added Index1.ID", "Added Index1.Out order"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Added Index1",{{"Added Index1.ID", "previous.ID"}, {"Added Index1.Out order", "previous.Out order"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Index.1"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each if [ID]=[previous.ID] and  [In order]<=[previous.Out order] then 0 else 1),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}}),
    #"Added Custom1" = Table.AddColumn(#"Sorted Rows", "Custom.1", each List.Sum(List.FirstN(#"Sorted Rows"[Custom],[Index])))
in
    #"Added Custom1"

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-juanli-msft ,

 

I used this solution too. It works good, but my report is very slow now. After this step I tried to do a groupby, but it's still loading. Did I something wrong?

 

Kind regards,

B. Yener

v-juanli-msft, thank you soo much, the given code worked fine. really i struggled a lot but you solved it. Thanks a lot.

I just want to know about .pbix file, which you have attahced. please let me know.

Is there any thing that i need to do from my end.

 

Mariusz
Community Champion
Community Champion

Hi @rajrajsha 

 

Can you paste your sample from excel into the body of the message? 

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi, I have copied sample data for your reference. Last column is the result that i expect.

Thanks for your reply.

Im sorry for the delay reply.

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