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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Kneuter
New Member

Need help with dynamic exception table for replacing values.

Hi all, 

 

I would like to write a PQ-script where a source table and an exception table are combined into a corrected table where the category is corrected based on a condition specified in the exception table.  

 

The  first column of the exception table specifies which column in the source table needs to be used to run the conditional statement through.

The second is the matching value. If the condition is true, than the category in the source table should be corrected. 

See example hereunder, anyone has an idea? 

 

It was formerly set up as a hardcoded conditional column but I would like to set it up as a table so it stays visible in a workbook and other people can add rows to the exception table and update the corrected table.  

 

Kneuter_0-1699626448444.png

 

Thanks for any help 

 

 

3 REPLIES 3
AlienSx
Super User
Super User

Hi, @Kneuter 

let
    source = your_source_table,
    exception = your_exception_table,
    e_lst = List.Buffer(Table.ToRows(exception)),
    txform = 
        Table.FromRecords(
            Table.TransformRows(
                source, 
                (r) =>
                    Record.TransformFields(
                        r,
                        {"Category", 
                        (x) => 
                            [poz = List.PositionOf(e_lst, r, Occurrence.First, (a, b) => Record.Field(b, a{0}) = a{1}),
                            out = if poz = -1 then x else e_lst{poz}{2}][out]}
                    )
            )
        )
in
    txform
ThxAlot
Super User
Super User

Simple enough, one line of code.

ThxAlot_0-1699708282961.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



OwenAuger
Super User
Super User

Hi @Kneuter 

I have attached an example of how you could handle this. I used Power Query in Excel since your screenshot appeared to be Excel, but this can be moved to Power BI if needed.

 

I have assumed that

  1. The first matching row (if any) from the Exception table will be used.
  2. In the Exception table, we never have Category = null.

 

My method is:

  1. First create queries from Source and Exception tables.
  2. Create a function fnReplacementCategory which is based on the Exception table. This function:
    1. Takes a record as input. The record will in practice be an individual row of the Source table.
    2. Finds the first row of Exception (if any) where the Column/Value pair exists in the record.
    3. If there is a matching row of Exception, return the value of Category from that row, otherwise null.
  3. Create an Output query which
    1. References the Source table.
    2. Applies the fnReplacementCategory function to each row, and if it returns a non-null value then replace Category by that value.

Below is the M code for fnReplacementCategory and Output:

// ============= fnReplacementCategory =====================================
let
  ExceptionList = Table.ToRecords(Table.Buffer(Exception)),
  ReplacementCategoryFunction = (InputRecord as record) =>
    let
      MatchingExceptions = List.Select(
        ExceptionList,
        each Record.FieldOrDefault(InputRecord, _[Column]) = _[Value]
      ),
      FirstMatch = List.First(MatchingExceptions),
      Category = FirstMatch[Category]?
    in
      Category
in
  ReplacementCategoryFunction

// ============= Output ===================================================
let
  Source = Source,
  #"Replace Category" = Table.ReplaceValue(
    Source,
    each [Category],
    each fnReplacementCategory(_),
    Replacer.ReplaceText,
    {"Category"}
  )
in
  #"Replace Category"

OwenAuger_0-1699703362853.png

Hopefully this is of some help, and can be tweaked if needed.

 

Regards


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

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.