COUNTIF in DAX for Power BI Card - powerbi

Here is my sample table (for this report I'm using only one table, so there is no table-relationship-links to contend with):
Cost Centre
Project
Invoice Approver
123
AB
Person One
123
AB
Person Two
123
ZZ
Person One
456
TB
Person Three
I have a measure already written Approver = COUNT('Table'[Invoice Approver]). In the sample above, Approver = 4.
I have created a new table showing only those combinations with 1 approver. The table shows 123|ZZ and 456|TB (NOTE: for this table, the approver(s) do not need to be shown, only the unique cost centre/project combinations).
My next step is to have a Card showing the net quantity of the filters: 2. But I need help in writing the DAX measure.
I have tried the following DAX formulae:
Code
Result
1InvApp = COUNTROWS(FILTER('Table',[Approver]="1"))
"DAX comparison operations do not support comparing values of type Integer with values of type Text."
1InvApp = COUNTROWS(FILTER('Table',[Approver]=1))
Blank (text)
1InvApp = CALCULATE(COUNTROWS('Table'),[Approver]="1")
"A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression."
1InvApp = CALCULATE(COUNTROWS('Table'),[Approver]=1)
"A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression."
1InvApp = COUNTX('Table',[Approver]="1")
"DAX comparison operations do not support comparing values of type Integer with values of type Text."
1InvApp = COUNTX('Table',[Approver]=1)
"The function COUNTX cannot work with values of type Boolean."
1InvApp = COUNTAX(FILTER('Table',[Approver]=1),'Table'[Approver])
Blank (text)
1InvApp = COUNTAX(FILTER('Table',[Approver]="1"),'Table'[Approver])
"DAX comparison operations do not support comparing values of type Integer with values of type Text."
1InvApp = COUNTROWS(FILTER(ALL('Table'),[Approver]=1)) (as suggested here)
Blank (text)
QUESTION: What is the correct DAX syntax to show the desired total result, 2?
Thanks in advance.

You can do this in two steps:
First:
Measure = CALCULATE(COUNT('Table'[Invoiced]), FILTER(ALL('Table'), SELECTEDVALUE('Table'[Cost Centre]) = 'Table'[Cost Centre] && 'Table'[Project] = SELECTEDVALUE('Table'[Project])))
Secod:
OnlyOne = CALCULATE(COUNTROWS(CALCULATETABLE('Table', FILTER('Table', var __xx = [Measure] return [Measure] = 1))))

Related

Using value from allselected in a measure to filter rows in another measure

I want to use the selected slicer value in string format to filter rows in a table in a visual.
So, I created the following measure:
SlicerVal = ALLSELECTED(Table[Column1])
The follow up measure is as follows:
TotalRows = CALCULATE(COUNTROWS(Table),filter(Table, Table[Column2] = [SlicerVal]))
However.. This returns completely different data. When hardcoding the string value the correct data is returned, like in the example below:
TotalRows = CALCULATE(COUNTROWS(Table),filter(Table, Table[Column2] = "A"))
Is there a way to convert the measure value to a string somehow, which can be used dynamically to correspond the string value filter to the selected slicer value?
How about this?
TotalRows = CALCULATE(COUNTROWS(Table),filter(Table, Table[Column2] IN ALLSELECTED(Table[Column1])))
[Question: are you intentionally filtering on all selected elements from Column1 when applying a conditions onto Column2? I have no idea about your use-case, that's why -- for safety reasons -- I'm asking...]

Filtering other tables based on cross-highlighting a filtered measure

I want to count records in a certain condition and allow people to filter down to the relevant records, but selecting a measure value (which filters so it only counts certain rows) isn't cross-filtering others as I'd expect. Maybe ths isn't possible or maybe I'm just doing it wrong, but I'd appreciate help.
I have a single table:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTJUitVBEjICChmgChljCplgajSFCMUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t, #"Ordered Recently" = _t]),
#"Change to INT" = Table.TransformColumnTypes(Source,{{"Ordered Recently", Int64.Type}}),
#"Change to T/F" = Table.TransformColumnTypes(#"Change to INT",{{"Ordered Recently", type logical}})
in
#"Change to T/F"
The result looks like this:
Customer Name Ordered Recently
Customer 1 True
Customer 2 False
Customer 3 False
Customer 4 True
Customer 5 True
I added two measures:
Count Total = COUNTROWS(Customers)
Count Recent = CALCULATE([Count Total], filter(Customers, Customers[Ordered Recently]))
If I put both measures in a bar chart and highlight the "Count Recent" measure, I'd expect it to know to filter other visuals based on the FILTER statement present in the measure, but that doesn't happen. Selecing this value doesn't impact anything else on my page (including just a count of rows).
The goal is to allow people to select a measure that counts rows and then to see the makeup of the data in those rows (select a count of late projects and filter other visuals to describe those late projects).
Is this possible or am I doing something wrong?
EXAMPLE:
Here's what it looks like now, with nothing selected:
When I select the black bar (the "Ordered Recently" measure), nothing changes right now - but here's what I want to happen (actually achieved with a slicer off screen on the T/F field):
I understand if my measure is a SUM of an integer field, it includes every row in the calculation - even when the row value is zero - and there's no way to filter my dataset based on that. However, in this case, my measure is actually using a FILTER on the dataset so that it only counts rows with a certain criteria set - given that, it should be able to filter the requested table, and then flow that filter through the rest of my dataset (the same way it would if I selected a bar from a chart where I had used that same field as the series - exactly how it works when I do this:
PBIX file to download as an example
No, I don't believe it's possible to make a measure value cross-filter other visuals based on filters within the measure definition.
You can, however, click on i.e. row header Customer 3 and it should cross-filter the other visuals to only include that customer. Any table column you set for the rows or columns of a matrix visual should behave this way.
Here's a hacky workaround:
Create a measure that shows the right values when you use the column you want to use as a filter as the Legend or Axis (like in your last image). For example, in this case, you could do this:
Total Customers =
VAR TF = SELECTEDVALUE ( Customers[Ordered Recently] )
RETURN
COUNTROWS (
FILTER (
ALLSELECTED ( Customers ),
IF ( TF, TF, TF || Customers[Ordered Recently] )
)
)
This behaves how you want, but isn't labeled as you want. To achieve that create a calculated column with the labels you want. For example,
Label = IF(Customers[Ordered Recently], "Ordered Recently", "Total Customers")
Then take Ordered Recently off the axis and put the Label column in the Legend box to get this:
Your Filter argument is really Filter(All(Customers, Customers[Ordered Recently])
You remove all filters on the Customer Table, and then specify Ordered Recently Column as the filter.
Try
[MeasureName] =Calculate([Count Total], All(Customer), Values(Customer[Recently Ordered]), Customer[Recently Ordered] = “True”)

I want a measure to avoid being filtered by a bar chat axis

I have a bar chart with a variable "MBA Type" in the axis.
"MBA Type" is a categorical variable with the following possible successive values: {MBA-LB, MBA1, MBA2, EMAP}
Values are calculated from the measure "Student variation", which should count the difference between the number of students in each MBA type and the number of students when "MBA Type" == LB.
Student variation should follow this logic: A - B, where A should be responsive to the Axis values, and B should have always "MBA Type"= MBA-LB
For illustration, you can find the following chart (what I currently have):
I would like the value corresponding to MBA-LB (17) to be subtracted from each bar. (i.e. MBA-LB=0, MBA1=-10, MBA2=-16, EMAP=-13)
Additionally, I would like to apply other filters to this visual. So I cannot use the following for calculating B:
B =
VAR
VAR_MBALB = FILTER(ALL('Table'), 'Table'[MBA Type] = "MBA-LB")
RETURN
CALCULATE(SUM('Table'[students]), VAR_MBALB)
I guess a solution might be to prevent B from being affected by the variable "MBA Type", and fix a specific value to it.
Any ideas on how I can do this?
Any comment or suggestions will be much appreciated.
Cheers!
See All function
ALL(Table) Removes all filters from the specified table.
ALL (Column[, Column[, …]]) Removes all filters from the specified
columns in the table; all other filters on other columns in the table
still apply.
So if you want to remove only the filter on MBA Type then use:
B =
VAR
VAR_MBALB = FILTER(ALL('Table'[MBA Type]), 'Table'[MBA Type] = "MBA-LB")
RETURN
CALCULATE(SUM('Table'[students]), VAR_MBALB)

Power BI Lookup with Duplicates Equation

I have a data set that contains duplicates and i am trying to do the equivalent of a Vlookup from excel. In excel when you use the vlookup function it will just return the first value even if there is a duplicate. The data set that i am working with has a unique 16 character string.
I have utilized some videos, forms, and other resources but no luck. I have used the calculation equation with a first non blank and a filter but i either get an error or returns blank.
https://1drv.ms/x/s!AtrxZbQBYb0LjZtaIkZcn4qsMimwnQ?e=PZbNud
Column = CALCULATE(
FIRSTNONBLANK('Table1'[ID]),
FILTER('Table1','Table1'[Parent]='Table1'[ID]))
You can go with:
Column =
var SampleID = 'Sample Data'[ID]
var EarliestDate = CALCULATE(MIN('Sample Data'[Creation Date]);FILTER('Sample Data';'Sample Data'[Parent] = SampleID))
return CALCULATE(MIN('Sample Data'[Text]);FILTER('Sample Data';'Sample Data'[Parent] = SampleID && 'Sample Data'[Creation Date] = EarliestDate))
Note: it finds the earliest date ann when dates are equal, smallest string
Enjoy!

How do you escape a column name in Google visualisation API query language?

I have a Google sheet which generates an error in the following expression:
=query(Capacity!A5:FE135,"SELECT C,A WHERE "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&" = '"&C2&"' AND "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&" = 1 ORDER BY C")
for a single, specific input value (a date) at D2.
Essentially, the purpose of the code is to find the column location of the date at D2 in a second sheet (Capacity) and put the values of that column in that sheet into column C in the current sheet, while also selecting only rows that match on a second column. When the date is set to a specific value, however, the expression will not evaluate.
On breaking this massive expression down into its component parts, it turns out the problem is caused by this expression:
=SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")
which, for the offending date, is returning column BY.
This means the expression being evaluated in Google Visualization API query language is:
SELECT C,A WHERE BY = '' AND BW = 1 ORDER BY C
but the query language sees BY as a reserved word, not a column, and barfs.
How can I escape the column name somehow to make it clear that it is to be considered a column name?
The way is to surround the offending portion with back-quotes (as I used to make text monospaced here):
=query(Capacity!A5:FE135,"SELECT C,A WHERE `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&"` = '"&C2&"' AND `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&"` = 1 ORDER BY C")
so the query will look like
SELECT C,A WHERE `BY` = '' AND `BW` = 1 ORDER BY C
I assume this will help when the sheet grows so big that we're on column IF as well.