excelpowerquerym

Excel Power Query syntax issue transform multiple columns of records


After many iterations I am progressing with the error messages, however, here I am running out of ideas.

I want to change several columns of a record, if the record fulfills a condition. If I do the operation for all records without checking a condition, it runs fine.

Statement without condition and no error

#"Adjustment" = Table.FromRecords(Table.TransformRows(#"NewClmns",
   (r) = >Record.TransformFields(r, List.Transform(DateRange, 
      (c) => {c, each _ * #"DownrateFactor"} ) ) )),

Statement with condition and error value of type "TransformOperations" expected

#"Adjustment" = Table.FromRecords(Table.TransformRows(#"NewClmns",
    (r) = >Record.TransformFields(r, 
       if r[#"Programname.2"] = "Qualified" then
          List.Transform( DateRange, 
             (c) => {c, each _ * 0.9}) 
       else Record.ToList(r) ) )),
   

This appears to come from the else statement, at least this is where I corrected the previous error (else r because r is a record and not a list)

=========================

Example

| Programmname.2 | 01012024 | 01022024 | 01032024 | 01042024 |
|----------------|----------|----------|----------|----------|
| abc            | 1        | 1        | 1        | 1        |
| Qualified      | 1        | 1        | 1        | 1        |
| abc            | 1        | 1        | 1        | 1        |

shall become

| Programmname.2 | 01012024 | 01022024 | 01032024 | 01042024 |
|----------------|----------|----------|----------|----------|
| abc            | 1        | 1        | 1        | 1        |
| Qualified      | 0.9      | 0.9      | 0.9      | 0.9      |
| abc            | 1        | 1        | 1        | 1        |

with

#"DateRange"  = List.RemoveFirstN(Table.ColumnNames(#"PrevStep"),1),

Solution

  • For what you show, it is relatively simple to use the Table.ReplaceValue function:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table8"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Programmname.2", type text}, {"01012024", Int64.Type}, {"01022024", Int64.Type}, 
            {"01032024", Int64.Type}, {"01042024", Int64.Type}}),
        
        #"Replace Qualified" = Table.ReplaceValue(
            #"Changed Type",
            each [Programmname.2],
            0.9,
            (x,y,z)as nullable number=>if y = "Qualified" then x*z else x,
            List.Skip(Table.ColumnNames(#"Changed Type"))
        )
    in
        #"Replace Qualified"
    

    enter image description here

    Note: To understand the assignments, see the MS Help Page for this function. But, in terms of the function arguments ColumnsToSearch => x, OldValue=>y, NewValue=>z. Of course, we could rewrite it as below to name those arguments. Might make it more clear:

    #"Replace Qualified" = Table.ReplaceValue(
            #"Changed Type",
            each [Programmname.2] = "Qualified",
            0.9,
            (columnsToSearch, isQualified, Factor) as nullable number=> if isQualified then columnsToSearch * Factor else columnsToSearch,
            List.Skip(Table.ColumnNames(#"Changed Type")))