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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Friedrich902
Regular Visitor

Get the first element from list which is not taken

Hello to everyone,

 

I want to take the first element from a list in the same row which was not taken from a row with a smaller index.

The Idea ist to match debit and credit bookings.

 

Do you know how the custom function in power query hat to be written?

 

In the sample data you can see a list where the first element should be taken but not twice.

 

Date | Booking Text | Transaction text | Account | Attribute | Value | IndexMatching Elements Index | Desired Result Custom Function

06.10.2021 00:00ACDTransactionADebit1209{7,9}7
06.10.2021 00:00ACDTransactionBDebit1203376{7,9}9
06.10.2021 00:00ACDTransactionCDebit5030415 null
06.10.2021 00:00ACDTransactionDDebit12040551{7,9}null

 

Thank you in advance.

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

Hello, @Friedrich902 

    s = your_table,
    lst = s[IndexMatching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} )

View solution in original post

2 REPLIES 2
Friedrich902
Regular Visitor

Hello AlienSX, than you so much for the solution. 

I have an additional problem.

 

I want to add a column "Desired Result Custom Function 2" which takes all elements from the list "All Elements Index" which are not taken in the new column "Result Custom Function 1". Is this possible?

 

DateBooking TextTransaction textAccountValueAttributeIndexAll Elements IndexMatching Elements IndexResult Custom Function 1Desired Result Custom Function 2
06.10.2021ACDTransaction ADebitAccountA1209{7,8,9}{7,9}7null
06.10.2021ACDTransaction ADebitAccountA1203376{7,8,9}{7,9}9null
06.10.2021ACDTransaction ADebitAccountA5030415{7,8,9}  8
06.10.2021ACDTransaction ADebitAccountA12040551{7,8,9}{7,9} 8

 

My M Code so far:

let
    Source = #table({"Date", "Booking Text", "Transaction text", "Account", "Value", "Attribute", "Index", "All Elements Index", "Matching Elements Index"}, {{#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 9, {7, 8, 9} , {7,9}}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 3376, {7, 8, 9} , {7,9}}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 50, 30415, {7, 8, 9} , null}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 40551, {7, 8, 9} , {7,9}
}}),

#"Changed_Type1" = Table.TransformColumnTypes(Source,{{"Booking Text", type text}, {"Transaction text", type text}, {"Account", type text}, {"Attribute", type number}, {"Date", type date}, {"Value", type text}, {"Index", Int64.Type}}),

s = #"Changed_Type1",

lst = s[Matching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} ),
    #"Changed Type" = Table.TransformColumnTypes(result,{{"Desired Result Custom Function", Int64.Type}})

    in 
    #"Changed Type"

 

AlienSx
Super User
Super User

Hello, @Friedrich902 

    s = your_table,
    lst = s[IndexMatching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} )

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors