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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
KuntalSingh
Helper V
Helper V

Need help on M code

This is my code 

let
Source = Excel.Workbook(File.Contents("C:\Users\KUNTALSINGH\Box\PepsiCo - NA Process Session\ACL_Input.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Scripts", type text}, {"Vendor", Int64.Type}, {"VendorName", type text}, {"BusinessArea", type any}, {"CompanyCode", type text}, {"FiscalYear", Int64.Type}, {"DocumentType", type text}, {"DocumentDate", type date}, {"PostingKey", Int64.Type}, {"PostingDate", type date}, {"DocumentNumber", Int64.Type}, {"Reference", type text}, {"Amountindoccurr", type number}, {"DocumentCurrency", type text}, {"Amountinlocalcurr", type number}, {"LocalCurrency", type text}, {"ClearingDocument", Int64.Type}, {"ClearingDate", type text}, {"Text", type text}, {"NetDueDate", type text}}),
#"Added Custom"= Table.AddColumn(#"Changed Type", "NewColumn",
each if [DocumentType] = "ZA" and [Text] <> "" then
Text.Combine({[DocumentType], [Text], Number.ToText(Number.Abs([Amountindoccurr]))})
else if [DocumentType] = "RE" and [Reference] <> "" then
Text.Combine({[DocumentType], [Reference], Number.ToText(Number.Abs([Amountindoccurr]))})
else
""
),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"NewColumn", type text}}),
#"Add Custom1" = Table.AddColumn(#"Changed Type1", "Condition", each
if Text.StartsWith([NewColumn], "RE") then
List.Count(List.Select(#"Changed Type1"[NewColumn], each Text.Replace([NewColumn], "RE", "ZA") = _))
else if Text.StartsWith([NewColumn], "ZA") then
List.Count(List.Select(#"Changed Type1"[NewColumn], each Text.Replace([NewColumn], "ZA", "RE") = _))
else
null
)
in
#"Add Custom1"

I am getting error 

Expression.Error: We cannot apply field access to the type Text.
Details:
Value=RE6727721V95.51
Key=NewColumn

 

ScriptsVendorVendorNameBusinessAreaCompanyCodeFiscalYearDocumentTypeDocumentDatePostingKeyPostingDateDocumentNumberReferenceAmountindoccurrDocumentCurrencyAmountinlocalcurrLocalCurrencyClearingDocumentClearingDateTextNetDueDateMatchCheck
S(60-87)-42000011432IOWA ROTOCAST PLASTICS INC US242024ZA26-06-20243128-06-20245500148255TPM0000003072664-2000USD-2000USD  TPM00000030726647/4/2024 
S(60-87)-52000011280NORTH STAR MARKETING INC US242023ZA01-09-20233105-09-20235500235337TPM0000003002117-1000USD-1000USD20000220059/10/2023TPM00000030021179/9/2023 
S(87-100)-52000204291BEVOLUTION GROUP (Corporate) US242024ZA02-06-20243105-06-20245500130371994623021-5882.1USD-5882.1USD  9946230216/15/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US242024ZA01-04-20243104-04-20245500084065813581021-7347.6USD-7347.6USD  8135810217/26/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US022024ZA16-02-20243120-02-20245500029608233740021-9655.8USD-9655.8USD  2337400216/10/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US042024ZA14-05-20243116-05-20245500024637261572021-9655.8USD-9655.8USD  2615720219/6/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US242024ZA07-03-20243109-03-20245500051722269201021-9655.8USD-9655.8USD  2692010216/30/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US022024ZA03-05-20243107-05-20245500061524377342021-9655.8USD-9655.8USD  3773420218/26/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US022024ZA09-05-20243114-05-20245500055226478062021-9655.8USD-9655.8USD  4780620219/2/2024 
S(87-100)-52000027747PLASTIPAK PACKAGING US022024ZA20-06-20243122-06-20245500079066535163021-9655.8USD-9655.8USD  53516302110/13/2024 
1 ACCEPTED SOLUTION

@KuntalSingh well, if you really want to use that key (type + some text columns + amount) and count the number of matching rows then try this after #"Changed Type" step:

    key= Table.AddColumn(
        #"Changed Type", "Name",
        each if [DocumentType] = "ZA" and [Text] <> "" 
        then Text.Combine({[DocumentType], [Text], Number.ToText(Number.Abs([Amountindoccurr]))})
        else 
            if [DocumentType] = "RE" and [Reference] <> "" 
            then Text.Combine({[DocumentType], [Reference], Number.ToText(Number.Abs([Amountindoccurr]))})
            else ""
    ),
    dict = Record.FromTable(Table.Group(key, "Name", {"Value", Table.RowCount})),
    rep = [RE = "ZA", ZA = "RE"],
    check = Table.AddColumn(
        key, 
        "Condition", 
        (x) => Record.FieldOrDefault(dict, Text.Replace(x[Name], x[DocumentType], Record.Field(rep, x[DocumentType])))
    )
in
    check

 

View solution in original post

14 REPLIES 14
Omid_Motamedise
Power Participant
Power Participant

I have run this code and do not face any error, it might be because of your column heder if you extract data from severl files

This is my code

let
Source = Excel.Workbook(File.Contents("C:\Users\KUNTALSINGH\Box\PepsiCo - NA Process Session\ACL_Input.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Scripts", type text}, {"Vendor", Int64.Type}, {"VendorName", type text}, {"BusinessArea", type any}, {"CompanyCode", type text}, {"FiscalYear", Int64.Type}, {"DocumentType", type text}, {"DocumentDate", type date}, {"PostingKey", Int64.Type}, {"PostingDate", type date}, {"DocumentNumber", Int64.Type}, {"Reference", type text}, {"Amountindoccurr", type number}, {"DocumentCurrency", type text}, {"Amountinlocalcurr", type number}, {"LocalCurrency", type text}, {"ClearingDocument", Int64.Type}, {"ClearingDate", type text}, {"Text", type text}, {"NetDueDate", type text}}),
#"Added Custom"= Table.AddColumn(#"Changed Type", "NewColumn",
each if [DocumentType] = "ZA" and [Text] <> "" then
Text.Combine({[DocumentType], [Text], Number.ToText(Number.Abs([Amountindoccurr]))})
else if [DocumentType] = "RE" and [Reference] <> "" then
Text.Combine({[DocumentType], [Reference], Number.ToText(Number.Abs([Amountindoccurr]))})
else
""
),
#"Add Custom1" = Table.AddColumn(
#"Added Custom",
"Condition",
each
if Text.StartsWith([NewColumn], "RE") then
let
NewColumnAdj = Text.Replace([NewColumn], "RE", "ZA")
in
List.Count(List.Select(#"Added Custom"[NewColumn], each NewColumnAdj = _))
else if Text.StartsWith([NewColumn], "ZA") then
let
NewColumnAdj = Text.Replace([NewColumn], "ZA", "RE")
in
List.Count(List.Select(#"Added Custom"[NewColumn], each NewColumnAdj = _))
else
null,
Int64.Type // specify column type
)
in
#"Add Custom1"

it does work for me again as bellow image
can you share the image of your error?


Omid_Motamedise_0-1725344884018.png

 

I am not getting any error. It took time to load file size inceases from MB to GB's. It is keeps on loading from last 15 mins

 

KuntalSingh_0-1725347292038.png

 

Ok. I thought you were having an error, I am going to look at the code and optimize it soon, just let me know how many rows are in your Excel file.

Thanks for your prompt response!

There are near around 70 K rows.

It is not to much but your formula for the condition column is not efficient as it going to apply a filter over all 70K rows, use the next formula, in this case, the result of the previous steps are summarized by group by (changed the row number from 70 k to lower rows), then this table is used for calculation of condition


let
Source = Excel.Workbook(File.Contents("C:\Users\n11472235\OneDrive - Queensland University of Technology\Desktop\qook2.xlsx"), null, true),

Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Scripts", type text}, {"Vendor", Int64.Type}, {"VendorName", type text}, {"BusinessArea", type any}, {"CompanyCode", type text}, {"FiscalYear", Int64.Type}, {"DocumentType", type text}, {"DocumentDate", type date}, {"PostingKey", Int64.Type}, {"PostingDate", type date}, {"DocumentNumber", Int64.Type}, {"Reference", type text}, {"Amountindoccurr", type number}, {"DocumentCurrency", type text}, {"Amountinlocalcurr", type number}, {"LocalCurrency", type text}, {"ClearingDocument", Int64.Type}, {"ClearingDate", type text}, {"Text", type text}, {"NetDueDate", type text}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"DocumentType", "Reference", "Amountindoccurr", "Text"}),
#"Added Custom"= Table.AddColumn(#"Removed Other Columns", "NewColumn",
each if [DocumentType] = "ZA" and [Text] <> "" then
Text.Combine({[DocumentType], [Text], Number.ToText(Number.Abs([Amountindoccurr]))})
else if [DocumentType] = "RE" and [Reference] <> "" then
Text.Combine({[DocumentType], [Reference], Number.ToText(Number.Abs([Amountindoccurr]))})
else
""
),
#"Grouped Rows" = Table.Group(#"Added Custom", {"NewColumn"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
#"Add Custom1" = Table.AddColumn(#"Added Custom", "Condition", each try #"Grouped Rows"{[NewColumn=(if Text.StartsWith([NewColumn], "RE") then Text.Replace([NewColumn], "RE", "ZA") else if Text.StartsWith([NewColumn], "ZA") then Text.Replace([NewColumn], "ZA", "RE") else null)]}[Count] otherwise null)
in
#"Add Custom1"

I tried the this code as well. It's keep on running since 12 PM till now. so this soluntion dos't work

@KuntalSingh well, if you really want to use that key (type + some text columns + amount) and count the number of matching rows then try this after #"Changed Type" step:

    key= Table.AddColumn(
        #"Changed Type", "Name",
        each if [DocumentType] = "ZA" and [Text] <> "" 
        then Text.Combine({[DocumentType], [Text], Number.ToText(Number.Abs([Amountindoccurr]))})
        else 
            if [DocumentType] = "RE" and [Reference] <> "" 
            then Text.Combine({[DocumentType], [Reference], Number.ToText(Number.Abs([Amountindoccurr]))})
            else ""
    ),
    dict = Record.FromTable(Table.Group(key, "Name", {"Value", Table.RowCount})),
    rep = [RE = "ZA", ZA = "RE"],
    check = Table.AddColumn(
        key, 
        "Condition", 
        (x) => Record.FieldOrDefault(dict, Text.Replace(x[Name], x[DocumentType], Record.Field(rep, x[DocumentType])))
    )
in
    check

 

Can you please help on one more logic

ReferenceAmountindoccurrColumn1Remove AlphapetsSumif AmountAmountcopy
128404JAIMES-5400 1284045400 5400
128404JAIMES5400 1284045400 5400
70015877987975.5 1284047975.5High Priority7975.5
70015877987975.5 1284047975.5High Priority7975.5
70015877987975.5 1284047975.5High Priority7975.5
11332716-5018.4 1284045018.4 5018.4
11332716-5018.4 1284045018.4 5018.4
 -10036.8 12840410036.8 10036.8

Please help on one more logic

same number in CC column have + and -

value in Amountindoccurr column means nullyfi then comment is blank and same number in CC column have  same  value either + or - in Amountindoccurr column then comment is Highy priority

 

Excel formula

1.Apply formula in  Column CC to remove alphabets from Reference(L Column) AND Concatenate with ABS(amt docu currency) after Please implement your logic to get the output

 

 

ScriptsVendorVendorNameBusinessAreaCompanyCodeFiscalYearDocumentTypeDocumentDatePostingKeyPostingDateDocumentNumberReferenceAmountindoccurrDocumentCurrencyAmountinlocalcurrLocalCurrencyClearingDocumentClearingDateTextNetDueDateConcatenationCC
S(25-60)-12004235596G&G OUTFITTERS INCUS052024RE4533131455305.1E+09242772-166.75USD-166.75USD   4/9/202451001530992024US05242772166.75
S(25-60)-12000011986G&G OUTFITTERS INCUS052024RE4533131453385.1E+09242772-166.75USD-166.75USD20005467724/14/20244/9/202451000427342024US05242772166.75
S(25-60)-12003871958CINTAS CORPORATION NUS012024RE4529331455305.1E+094178789859-205.94USD-205.94USD   3/2/202451018370002024US014178789859205.94
S(25-60)-12000029014CINTAS CORPORATION 100US012024RE4529331455105.1E+094178789859-205.94USD-205.94USD   3/2/202451005247402024US014178789859205.94
S(25-60)-12003871958CINTAS CORPORATION NUS012024RE4530731455305.1E+094180383750-209.39USD-209.39USD   3/16/202451000625092024US014180383750209.39
S(25-60)-12000011956CINTAS CORPORATION NO 2US012024RE4530731455065.1E+094180383750-209.39USD-209.39USD   3/16/202451009005242024US014180383750209.39
S(25-60)-22000014020NYSCO PRODUCTS LLCUS052024RE4459221455305.1E+090236725-IN91.4USD91.4USD  VR:Full Accrual4/1/202251008240022024US050236725-IN91.4
S(25-60)-22000014020NYSCO PRODUCTS LLCUS052024RE4459231454575.1E+090236725-IN-91.4USD-91.4USD    51010235452024US050236725-IN91.4
S(25-60)-22000022228W W GRAINGERUS012024RE4500721455305.1E+099648709799217.97USD217.97USD  VR:Pay-as-billed7/20/202351003365112024US019648709799217.97
S(25-60)-22000022228W W GRAINGERUS012024RE4500731454225.1E+099648709799-217.97USD-217.97USD  VR:Pay-as-billed7/20/202351012950142024US019648709799217.97
S(25-60)-22004015811CINTAS CORPORATION NUS052024RE4528131455305.1E+09I312210921-234.72USD-234.72USD  VR:Pay-as-billed2/19/202451024855002024US05I312210921234.72
S(25-60)-22000031331CINTAS FIRE PROTECTIONUS242024RE4528121453205.1E+09I312210921234.72USD234.72USD1008315074/3/2024VR:Pay-as-billed2/19/202451000080072024US24I312210921234.72
S(25-60)-22000031331CINTAS FIRE PROTECTIONUS242024RE4528131452995.1E+09I312210921-234.72USD-234.72USD1008315074/3/2024VR:Pay-as-billed2/19/202451000540022024US24I312210921234.72
S(25-60)-22000022228W W GRAINGERUS012024RE4528131455305.1E+099943375106-207.97USD-207.97USD   4/19/202451003750022024US019943375106207.97
S(25-60)-22000022228W W GRAINGERUS012023RE4528131452885.1E+099943375106-207.97USD-207.97USD   4/19/202451013625832023US019943375106207.97
S(25-60)-22000022228W W GRAINGERUS012024RE4528131454575.1E+099943375106-207.97USD-207.97USD   4/19/202451011440152024US019943375106207.97
S(25-60)-22000022228W W GRAINGERUS012024RE4528121454885.1E+099943375106207.97USD207.97USD  MR8M4/19/202451005320102024US019943375106207.97
S(25-60)-22000022228W W GRAINGERUS012024RE4528121454275.1E+099943375106207.97USD207.97USD  MR8M4/19/202451006805052024US019943375106207.97
S(25-60)-22003871958CINTAS CORPORATION NUS012024RE4530731455305.1E+094180383750-209.39USD-209.39USD   3/16/202451000625092024US014180383750209.39
S(25-60)-22000011956CINTAS CORPORATION NO 2US012024RE4530731455065.1E+094180383750-209.39USD-209.39USD   3/16/202451009005242024US014180383750209.39
S(25-60)-22000011956CINTAS CORPORATION NO 2US012024RE4530721455075.1E+094180383750209.39USD209.39USD  MR8M3/16/202451014410102024US014180383750209.39
S(25-60)-22000011956CINTAS CORPORATION NO 2US012024RE4530731455075.1E+094180383750-209.39USD-209.39USD20001180108/4/2024 3/16/202451005250672024US014180383750209.39
S(25-60)-22000011956CINTAS CORPORATION NO 2US012024RE4530721455075.1E+094180383750209.39USD209.39USD20001180108/4/2024MR8M3/16/202451008885072024US014180383750209.39
S(25-60)-22000029014CINTAS CORPORATION 100US012024RE4530731455105.1E+094180383750-209.39USD-209.39USD   3/16/202451004805942024US014180383750209.39

Perfect! Thank you very much for your kind support 🙂

OwenAuger
Super User
Super User

Hi @KuntalSingh 

Try updating the last step #"Add Custom1" as follows:

 

  #"Add Custom1" = Table.AddColumn(
    #"Changed Type1",
    "Condition",
    each
      if Text.StartsWith([NewColumn], "RE") then
          let
            NewColumnAdj = Text.Replace([NewColumn], "RE", "ZA")
          in
            List.Count(List.Select(#"Changed Type1"[NewColumn], each NewColumnAdj = _))
      else if Text.StartsWith([NewColumn], "ZA") then
          let
            NewColumnAdj = Text.Replace([NewColumn], "ZA", "RE")
          in
            List.Count(List.Select(#"Changed Type1"[NewColumn], each NewColumnAdj = _))
      else
        null,
    Int64.Type // specify column type
  )

 

 The issue was that within List.Select, the field reference [NewColumn] doesn't refer to the field [NewColumn] in the record corresponding to the current row, but rather it tries to refer to the field [NewColumn] of the current items in the list (which is the list of values int he column #"Changed Type1"[NewColumn].

 

The code above fixes this by placing Text.Replace([NewColumn],...) outside List.Select and storing in NewColumnAdj.

 

Does this work as intended?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thank you very much for your prompt support

I have more than 65 K rows data and it takes time to load data. Please assist how to load data faster

Helpful resources

Announcements
September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Top Solution Authors