Replicate "Countif" in PowerBI using DAX - dax

I am a new user to DAX and Power BI, but I am familiar with Excel. I want to replicate these countif formulas in DAX. In Excel, they are counting how many times a specific text string (in this case, the name of a brand) appears in the column, for example:
=COUNTIF(BH2:BH31,"Brand_A"), it is counting how many times the text "Brand_A" appears in the selection.
and I would like to know how I can do this in DAX in PowerBI. If anyone would be interested in providing some sample code I could try out, that would be very helpful.

You will likely want something like the COUNTX or COUNTAX function, combined with a FILTER, to replicate the functionality of Excel's COUNTIF.
https://learn.microsoft.com/en-us/dax/countax-function-dax
https://learn.microsoft.com/en-us/dax/countx-function-dax
Eg.
=COUNTAX(FILTER('YourTable',[BrandColumn]="Brand_A"),[BrandColumn])
Power BI's different "COUNT" functions have slightly different criteria in terms of whether a row gets counted or not (based on whether it's considering purely "empty" cells, or how the expression is evaluated), so you'd need to check the docs for each function and work out which one suits your specific requirement
(And by the way, a Google search of "Power BI COUNTIF" will give you plenty of results where you will find a range of different examples that should help)

You can use this calculation (COUNTX may be slow, because its a iterator) :
CountIf = CALCULATE( COUNTROWS('YourTable' ), FILTER(ALL('YourTable'), 'YourTable'[Brand] = "YourBrand"))

Related

Is there a faster way to increase/decrease a value besides using scripts in Google Sheets?

https://docs.google.com/spreadsheets/d/1wgapukUmnkgW3qZLqZ8I-FgRI1kCBw4tYGNBJnxIv_Q/edit?usp=sharing
You can see I created up and down arrows that are used to increase/decrease the percentages in column H.
It's a little bit slow because it's run by a script. Is there a faster way to do this?
You can use Data Validation for the values in your I column
The formula for your named range could be
=ArrayFormula(SEQUENCE(11,1,14,2)/100)
As for the Data Validation you can use List from a range and use my_percentages.
Using the combination of Named ranges and Data validation will give you instant results.
(Make a note that the named range can be anywhere. Even in a new tab.)
Functions used:
ArrayFormula
SEQUENCE

Filter Data for Each Row in a Column

EVE Online Manufacturing Spreadsheet
In Batch!F3:G, I'm attempting to break down the data input from columns B3:C to their components (and eventually materials/minerals in I3:J) by using filter to compare results in Engine!P:R. Multiplied of course by the total number of each finished product I need.
I've been trying to figure out ways to arrayformula this together, and even tried quite a few query functions without success. The best I've been able to come up with is to string the actual formula together, appending them with {}, but this gets bloated quickly. I need this to be open ended because I have a tendency to build a lot of things at once. Any help would be appreciated, even just point me in the right direction!
Well, based on my limited knowledge about google sheet, I can only think of one way to do this automatically.
Here's a sheet I constructed based on your sheet.
https://docs.google.com/spreadsheets/d/1AfX8o05gUGPiN5S90w4o0yxuIYjsJRaXsaYUFTJuEPo/edit?usp=sharing
First, on Engine sheet, add one more column which will give you the number of materials required for that part, which is looked up in the PART LIST of BATCH sheet. For this I use VLOOKUP, as you see in D2.
Then on BATCH sheet, query the materials that VLOOKUP return positive, multiply it by the amount of item and then sum them.
This is done by the QUERY used in F3
This method only if you don't have duplicate item in your PART LIST, due to the way VLOOKUP work.
Of course if you want to break the material list further, you can do the same approach..

DAX COUNT/COUNTA functions

I've looked at many threads regarding COUNT and COUNTA, but I can't seem to figure out how to use it correctly.
I am new to DAX and am learning my way around. I have attempted to look this up and have gotten a little ways to where I need to be but not exactly. I think I am confused about how to apply a filter.
Here's the situation:
Four separate queries used to generate the data in the report; but only need to use two for the DAX function (Products and Display).
I have three columns I need to filter by, as follows:
Customer (Display or Products query; can do either)
Brand (Products query)
Location (Display query)
I want to count the columns based on if the data is unique.
Here's an example:
Customer: Big Box Buy;
Item: Lego Big Blocks;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Lego Star Wars;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Surface Pro;
Brand: Microsoft;
Location: Electronics;
BREAK
Customer: Little Shop on the Corner;
Item: Red Bicycle;
Brand: Trek;
Location: Racks;
In this example, no matter the fact that the items are different, we want to look at just the customer, the brand, and the location. We see in the first two records, the customer is "Big Box Buy" and the brand is "Lego" and the location is "Toys". This appears twice, but I want to count it distinct as "1". The next "Big Box Buy" store has the brand "Microsoft" and the location is "Electronics". It appears once and only once, and thus the distinct count is "1" anyway. This means that there are two separate entries for "Big Box Buy", both with a count of 1. And lastly there is "Little Shop on the Corner" which appears just once and is counted just once.
The "skeleton" of the code I have is basically just to see if I can get a count to work at all, which I can. It's the FILTER that I think is the problem (not used in the below example) judging by other threads I've read.
TotalDisplays = CALCULATE(COUNTA(products[Brand]))
Obviously I can't just count the amount of times a brand appears as that would give me duplicates. I need it unique based on if the following conditions are met:
Customer must be the same
Brand must be the same
Location must be the same
If so, we distinctly count it as one.
I know I ranted a bit and may seem to have gone in circles, but I was trying to figure out how to explain it. Please let me know if I need to edit this post or post clarification.
Many thanks in advance as I go through my journey with DAX!
I believe I have the answer. I used a NATURALINNERJOIN in DAX to create a new, merged table since I needed to reference all values in the same query (couldn't figure out how to do it otherwise). I also created an "unique identity" calculated column that combined data from multiple rows, but was hidden behind the scenes (not actually displayed on the report) so I could then take a measure of the unique values that way.
TotalDisplays = COUNTROWS(DISTINCT('GD-DP-Merge'[DisplayCountCalcCol]))
My calculated column is as follows:
DisplayCountCalcCol = 'GD-DP-Merge'[CustID] & 'GD-DP-Merge'[Brand] & 'GD-DP-Merge'[Location] & 'GD-DP-Merge'[Order#]
So the measure TotalDisplays now reports back the distinct count of rows based on the unique value of the customer ID, the brand, and the location of the item. I also threw in an order number just in case.
Thanks!
I am semi new to DAX and was struggling with Count and CountA formula, you post has helped me with answers. I would like to add the solution which i got for my query: Wanted count for Right Time start Achieved hence if anyone is looking for this kind of answer use below, filter will be selecting the table and adding string which you want to
RTSA:=calculate(COUNTA([RTS]),VEO_Daily_Services[RTS]="RTSA")

Qlik sense - Rank() within a specific dimension when you have multiple ones

I am new to Qlik and trying to solve the following issue.
I have a table with two dimensions, one with the entry's unique ID, and one with a category, as in the example below.
Table example
My goal is to create a new column with a ranking of 'Score' - my measure - per category:
Table with desired output
If I use the expression
Rank(Score)
I get a column of ones, as the command takes the most granular dimension (Unique ID) as the default one. If I use
Rank(TOTAL Score)
It obviously returns a ranking regardless of all the dimensions. By reading the documentation and similar questions asked by other users I reckon that it should be possible to specify which dimension to use for TOTAL, with the following syntax:
Rank(TOTAL <Category> Score)
Yet, the formula returns an error and only null column values. I've tried different syntax, use of brackets but I still cannot grasp what I am doing wrong.
Please note that I cannot create the ranking column when loading the data.
I would immensely appreciate if someone were so kind to help on this!
Try with
=aggr(rank(sum(Score)), Category, UniqueID)

Calculate Value For Dates Between

A few references:
Microsoft's documentation on DATESBETWEEN.
Somewhat similar question, though the answer and derivatives of the formula don't return the correct results.
Microsoft's documentation on TODAY
Per the above Microsoft documentation, I'm trying to get a calculation for the last three months based on today's current date in SSAS Tabular model. First, I have no idea how to use SSAS and my company doesn't provide any learning material, so I've been reading through the MSDN documentation, which may not be the place to start, so if this is wrong, I'd appreciate being told so. For instance, with C# or Ruby, I can test code in a console to see if I get the result that I want, and I don't see how I can do that in SSAS Data Tools' DAX language - this is a GUI which gives users very little power over what they can do (it took me four hours to figure out how to access a dimension's properties). I am definitely a code monkey.
I tried using the below formula (and derivatives of it) because this is what it looks like Microsoft is doing in their example:
3MonthValue:=CALCULATE(SUM([MeasureOne])/SUM([MeasureTwo]),DATESBETWEEN(DateDimension[Date],DATEADD(DateDimension[Date],-3,MONTH),TODAY()))
The result, nothing. Of course, if I run similar SQL logic, I get the right results. I also used the provided SO example, though I suspect that's not exactly what I'm trying to achieve, and only obtained blanks as answers. Given that I need to calculate a formula between a certain time frame, which in SQL would be the WHERE clause, how do I translate this into DAX? In other words, what is DAX's WHERE and if CALCULATE isn't right, what's the correct approach?
When you say it isn't working, how do you mean? The formula you are using refers to your date dimension's key as the starting date for your DATESBETWEEN function - this means if you are expecting the measure to populate a value, you'll need to be using a particular date in your pivot to establish context.
If you are trying to view the measure at design time, in the editor, there is no context so the measure wont populate.
Moreover, if in a pivot you're looking at a time context that includes more than one date, that also will not work. So say you are looking at a month, or a quarter. Both of these encompass what amount to multiple DateDimension[Date]'s - so again context cannot be established.
so to recap - measures which look at date ranges like DATESBETWEEN using a starting time context that is set to your dimensions time key will only show up in a pivot when the pivots data is filtered to a single date.
You can test this using the same function, but hard set the starting date by replacing DateDimension[Date] with a static date (or possibly TODAY()). The measure should show up in design time because the formula has all the information it needs to complete the calculation.

Resources