Forum Discussion
Conditional calculation of new column in Power Query
- 2 years ago
Hi sara11,
There are multiple ways to achieve this, here are two.let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpPLEktUgAyzE0NgGRiolKsDkQmuDQ9ESxjoGNoBKRU4TLOGam5mcmJOQoRCmCdRuY6xkA6Nx1TRZQCxAQjkAkQeSeYrUDaRMcSSacTzFYgbapjZAG31AnFUrCsgSmqPoSNQI6xgQHEulgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sample = _t, Parameter = _t, Result = _t, Unity = _t]), ChType = Table.TransformColumnTypes(Source,{{"Result", type number}}, "nl-NL"), AddedCustom = Table.AddColumn(ChType, "Custom", each [ t = Table.FindText( ChType, [Sample] ), a = if [Parameter] = "Water " and [Unity] = "aa" then List.Product( Table.SelectRows(t, each [Parameter] ="Water " or [Parameter]="Sugar ")[Result]) /10 else null ][a] ) in AddedCustomor a Group By
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpPLEktUgAyzE0NgGRiolKsDkQmuDQ9ESxjoGNoBKRU4TLOGam5mcmJOQoRCmCdRuY6xkA6Nx1TRZQCxAQjkAkQeSeYrUDaRMcSSacTzFYgbapjZAG31AnFUrCsgSmqPoSNQI6xgQHEulgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sample = _t, Parameter = _t, Result = _t, Unity = _t]), ChType = Table.TransformColumnTypes(Source,{{"Result", type number}}, "nl-NL"), GroupedRows = Table.Group(ChType, {"Sample"}, { {"t", each Table.AddColumn(_, "Custom", (x)=> if x[Parameter] = "Water " and x[Unity] = "aa" then List.Product( Table.SelectRows(_, (x)=> x[Parameter] ="Water " or x[Parameter]="Sugar ")[Result]) /10 else null ) } } ), Combine = Table.Combine( GroupedRows[t] ) in Combinewith this result
I hope this is helpful
Yes, I understand.
However there doesn't appear to be any self referencing within the shared code. Therefore I am wondering if Table4 points to this query - the one for which you have shared the code - because that will generate:
Expression.Error: A cyclic reference was encountered during evaluation.
Hmm... Yeah. Maybe. I can't say. I don't know much about it, unfortunately.
If so, what can I do to reverse it?
- m_dekorte2 years agoResident Rockstar
Look for the query name (for which you shared the code) inside Table4 's code - if that name includes a space, special character or keyword the quoted notation will be used. To illustrate, let's say it is called: my Query, because this includes a space it will show up as #"my Query" in the M code inside Table4 but without a space myQuery will show as myQuery
To resolve it you have to determine which comes first and restore the Source step in that query to access the data that you need to transform.
Alternatively you may be able to combine the logic from these two queries into one, but that is only possible if you do not require them as separate tables elsewhere in your workflow.