DAX problem, filter/relation is ignored when I use IF in the RETURN clause - powerbi

I'm getting an unexpected result when I use an IF in the RETURN clause of a DAX expression. If I don't use the IF, but instead just a variable, then the result is ok.
I've created a test scenario to explain my problem:
I have two test tables:
Table: "Test Object"
Table: "Test Group"
These have a unidirectional relation on "Group code"
I have created a measure "Test measure":
This gives the correct result:
I have set a page filter to only show Group Code "G01".
This all works ok up to this point.
But it goes wrong when I use an IF function:
I then get the following (incorrect) result. Apparently the relation and/or page filter seems to be ignored now:
NB: The result is the same regardless of from which table I use the "Group code" field.
What am I missing here?
I've created a PBIX file that shows the problem:
https://www.dropbox.com/s/76ld1kv503ul6nm/DAX%20problem%20with%20IF.pbix?dl=0

This is called "Auto-Exist" in PBI:
https://www.sqlbi.com/articles/understanding-dax-auto-exist/
If you look closer to the results, you'll notice that your report shows all possible combinations between Group Codes and Object Codes.
This is happening whenever you use a combination of fields from the different tables in a report: PBI first creates a cross-join between these fields, and then eliminates those combinations that result in blanks, so you only see meaningful combinations.
However, you IF statement overrides this logic - you are returning a result always, even if a combination is blank (Blank < 40 test returns "low end" because blank is treated as zero).
To fix it, calculate results only if the variable is not blank, i.e:
Price category =
var lowestPrice = MIN(Object[Price])
var result = IF( NOT ISBLANK(lowestPrice), IF(lowestPrice < 40, "Low end", "High end"))
Return result
You will get:
P.S. Page filter is irrelevant here, it simply filters the table after it's calculated.

Related

Changing measures with a slicer: "a table of multiple values was supplied where a single value was expected" error message

I followed this blog's instructions to create a slicer that would let me swap between measures in a text table: https://community.powerbi.com/t5/Community-Blog/Dynamically-change-the-information-within-a-visual-via-a-slicer/ba-p/87027
I am getting this error message for the column I am creating in the last step: "a table of multiple values was supplied where a single value was expected":
Select level (% favorable) =
SWITCH( TRUE(),
VALUES ('Measure Dimensions'[Measure]) = "Country", ROUND([c_pct_favorable],1),
VALUES ('Measure Dimensions'[Measure]) = "State", ROUND([s_pct_favorable],1),
VALUES ('Measure Dimensions'[Measure]) = "Zip", ROUND([z_pct_favorable],1),
BLANK())
I am basically trying to swap between country, state, and zip code level % favorable survey data.
Note: I had to add the ROUND calculation because I was getting a 'you can't combine fields with different data types' error message.
I tried Googling this error message but didn't find a use case exactly like mine and couldn't figure out the other article's logic and how it applied to my problem.
Thank you very much for your help!

PowerBi Dax - Create a Measure that ignores applied filters and display in barchart

In my data I have two columns of date - claim registration date and resolved date. My report is using resolved date as a slicer filter. I would like to build a bar chart showing registered claims by client segments. I have tried several approaches and functions but they all return single count value. What I want is actual counted values for each type.
BySegmentRegistered = CALCULATE(COUNT(claims_data[client_id]),claims_data[reg_date].[MonthNo] == MONTH(SELECTEDVALUE(DateTable[MonthYear])),ALL(claims_data))
BySegmentRegistered = CALCULATE(COUNT(claims_data[client_id]),FILTER(ALL(claims_data),claims_data[reg_date].[MonthNo] == MONTH(SELECTEDVALUE(DateTable[MonthYear]))))
I have tried above code and several other iterations but they all return single value across all client_segments. If I simply do COUNT(claims_data[client_id]) than it displays count by each segment but date is wrong, hence it doesnt work for me.
Any ideas?
EDIT:
I just tried this and it works.
BySegmentRegistered = CALCULATE(COUNT(claims_data[cliend_id]), claims_data[reg_date].[MonthNo] == MONTH(SELECTEDVALUE(DateTable[MonthYear])), REMOVEFILTERS(DateTable[MonthYear]))

Conditionally Filtering Out Rows based on 2 Parameters in w/ Power Query

I have a table similar to the one attached below:
What I would like to do, using power query, is conditionally filter out (remove) rows where CenterNum = 1101 and DepCode = 257. I figured Table.SelectRows() would work but it doesn't and the query just returns, this table is empty. The #"Expanded AccountLookup" ,in my formula below, is referencing the power query applied step before the one I am trying to create. I'm hoping to get some input on how to remove rows based on these two paramters.
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = "1111001" and [NoteTypeCode] = "257")
Thank you!
You didn’t post a screenshot so it is hard to tell if the column format is text or numerical but try removing the quotes around the numbers
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = 1111001 and [NoteTypeCode] = 257)
If that doesn't work, check the actual column names against what you are using, especially for case (upper/lower) and leading/trailing spaces. The best way to do that is to temporarily rename it, and look at the code for the "from name"

How to Perform a Full-Outer Join on Two Separate, Filtered Tables using DAX?

I have an original table named Error, and two additional tables (ErrorBefore and ErrorAfter) derived from the original (e.g. ErrorAfter = ALLSELECTED('Error')). I want to compare values from a 'before' version with an 'after' version, with the different version picked by slicer with 'Single select' on. That's working okay. Now I want to perform a full-outer join on the two results, joining on the Message column. The image below shows the result I have so far, with a fabricated table at the bottom of what I'm trying to achieve. I've tried using NATURALLEFTOUTERJOIN and GENERATE but they either don't give the result that I seek. Does anyone know how to perform the join?
PBIX share here.
First, change your data model to this:
I removed all your derived tables and relations, and instead created 2 tables like this:
Version Before = DISTINCT('Error'[Version])
Version After = DISTINCT('Error'[Version])
Both tables should have no relations with the Error table.
Then, create a measure:
Message Count = COUNT('Error'[Message])
You should always create measures yourself, never use Power BI auto-aggregations.
Next, create a measure for "Before" count"
Message Count Before =
VAR Version_Before = SELECTEDVALUE('Version Before'[Version])
RETURN
CALCULATE([Message Count], 'Error'[Version] = Version_Before)
and, similarly:
Message Count After =
VAR Version_After = SELECTEDVALUE('Version After'[Version])
RETURN
CALCULATE([Message Count], 'Error'[Version] = Version_After)
Finally, adjust your visuals:
Slicer "Before" should be based on table "Version Before"
Slicer "After" should be based on table "Version After"
Charts and tables should use "Message Count Before" and "Message Count After" measures in values
Add another table with messages and both measures
Result:

Trying to modify read-only DataSet field

I use C++ Builder 6.0
I use TADODataSet execute following SQL statement:
SELECT Id, SUM(Saldo) AS Saldo
FROM Table
GROUP BY Id
I use this DataSet only for reporting. No need update date back to database.
When I try to modify field "Saldo"
adospCard->Edit();
adospCard->FieldByName("Saldo")->AsFloat=0.0;
adospCard->Post();
I get error:
Field 'Saldo' cannot be modified.
I add this line
adospCard->FieldByName("Saldo")->ReadOnly=false;
and error no more occurred, but field 'Saldo' has not changed.
adospCard->Edit();
//adospCard->FieldByName("Saldo")->AsFloat=1536.5
adospCard->FieldByName("Saldo")->AsFloat=0.0;
//adospCard->FieldByName("Saldo")->AsFloat=0
adospCard->Post();
//adospCard->FieldByName("Saldo")->AsFloat=1536.5
Howe to change ‘Saldo’ field value?
Add a calculated field to your dataset.
Calculate the right value for Saldo
in this calculated field (you can
use Saldo as source for it if you
want to)
display this calculated field in your report in
stead of the Saldo field.
Edit:
For examples of how to add calculated fields, see for instance here, here and here.
--jeroen
As ldsandon said, you cannot modify the "Saldo" field as it is computed.
If you need to set the value to zero when Id is "something" you are better off doing it in your query. The best approach depends on the criteria for setting the value to zero.
Or, save the results of the original query in a temp table then modify that before returning the results to the report.
Finally, what reporting tool are you using? Can that do the "Saldo = 0" change when rendering the report?
Consider storing your result in a ClientDataset - if you can be assured the result isn't too large.
I don't use "TADODataSet" so the following may not apply :)...
When I do the same (or similar) using my DB of choice (i.e. Advantage Database Server) I would use the INTO clause, albeit, with a TSQLQuery component (with the RequestLive property enabled). For example:
SELECT Id, SUM(Saldo) AS Saldo INTO #TempTable FROM Table GROUP BY Id