March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
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.
Thanks for any help
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
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
My method is:
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"
Hopefully this is of some help, and can be tweaked if needed.
Regards
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
34 | |
30 | |
20 | |
19 | |
12 |