简体   繁体   中英

Powerquery: how to iterate/loop parameters list?

How to substitute code below with compact loop?

let
    ParametersList = {"CustomerID","FirstName","LastName"},
    Source1 = fnCheckId(srcTbl , ParametersList{0}),
    Source2 = fnCheckId(Source1, ParametersList{1}),
    Source3 = fnCheckId(Source2, ParametersList{2}),
    Result = Source3    
in
    Result

Looping Problems :

  1. It should loop ParametersList
  2. current loop output table should work as input Table for next loop

SIMPLIFIED EXAMPLE DETAILS ( Source File ):

fnCheckId function example (in real business case much more complex):

(tbl as table, clm as text)=>
let   
    //tbl = srcTbl, clm = "FirstName",
    #"Added Custom" = Table.AddColumn(tbl,"QA "&clm, each if Text.Length(Record.Field(_, clm))>3 then "Ok" else "Nok")    
in
    #"Added Custom"

Source Table:

Table.FromRows(
        {
            {1, "Bob", "Smith", "123-4567"},
            {2, "Jim", "Brown", "987-6543"},
            {3, "Paul", "Wick", "543-7890"}
        },
        {"CustomerID", "FirstName", "LastName", "Phone"}
    )

在此处输入图像描述

Estimated Result Table: 在此处输入图像描述

Please try this loop example:

  1. loops parameters list
  2. Use function output table as input for next loop
  3. Do NOT require knowledge about looping function fnCheckId

function Loop_fnCheckId :

= (Loop as number, inTbl as table, inPrm as list )=>
try       Loop_fnCheckId(Loop-1, fnCheckId(inTbl, inPrm{Loop-1}),inPrm)
otherwise inTbl

code with loop :

let 
    ParametersList = {"T","PR","Fen","XAL"},    
    cntLoop = List.Count(ParametersList),
    Result = Loop_fnCheckId(cntLoop,SourceTable, ParametersList)
in  Result

PS The question: is it possible to rewrite it using each _ syntax and avoid additional looping function Loop_fnCheckId

With original function, how about

let srcTbl = Table.FromRows(
    {
        {1, "Bob", "Smith", "123-4567"},
        {2, "Jim", "Brown", "987-6543"},
        {3, "Paul", "Wick", "543-7890"}
    },
    {"CustomerID", "FirstName", "LastName", "Phone"}
),
List = {"CustomerID", "FirstName", "LastName"},
#"Unpivoted Only Selected Columns" = Table.Unpivot(srcTbl, List, "Attribute", "Value"),
Source1 = fnCheckId(#"Unpivoted Only Selected Columns","Value"),
#"Removed Columns" = Table.RemoveColumns(Source1,{"Value"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"QA Value", "Value"}}),
#"Change Title" = Table.TransformColumns(#"Renamed Columns",{{"Attribute",each "QA" & _, type text}}),
combined =  #"Unpivoted Only Selected Columns" & #"Change Title",
#"Changed Type" = Table.TransformColumnTypes(combined,{{"Attribute", type text}, {"Value", type text}}) ,
#"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Attribute]), "Attribute", "Value")
in  #"Pivoted Column"

or with changed function as below,

let srcTbl = Table.FromRows(
    {
        {1, "Bob", "Smith", "123-4567"},
        {2, "Jim", "Brown", "987-6543"},
        {3, "Paul", "Wick", "543-7890"}
    },
    {"CustomerID", "FirstName", "LastName", "Phone"}
),
List = {"CustomerID", "FirstName", "LastName"},
#"Unpivoted Only Selected Columns" = Table.Unpivot(srcTbl, List, "Attribute", "Value"),
#"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Only Selected Columns",{{"Value", type text}}),
#"Processed" = Table.TransformColumns(#"Changed Type1",{{"Value",each fnCheckId2(_), type text}}),
Namechange = Table.TransformColumns(Processed,{{"Attribute",each "QA "&_, type text}}),
combined = #"Changed Type1" & Namechange,
#"Pivoted Column" = Table.Pivot(combined, List.Distinct(combined[Attribute]), "Attribute", "Value")
in  #"Pivoted Column"

with fnCheckId2

( clm as text)=>
let   
z = if  Text.Length(clm )>3 then "Ok" else "Nok"
in z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM