In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-
Related
I have a dataset and I want to create a column(not measure) to calculate the count of customers in each month. I don't know how I can count each customer once a month in Power BI.
I wrote this code but it counts the number of frequent customers more than once a month.
myCol = CALCULATE( DISTINCTCOUNT('table'[user_id] ) , 'table'[order_date] )
For example, it's my data:
The true result should be:
but my code returns this result:
How should I write the code for this calculating column to get a true result?
Since you are trying to calculate per month, you need a "year_month" column.
Then:
count_of_customer =
CALCULATE(
DISTINCTCOUNT('table'[user_id]),
ALLEXCEPT('table', 'table'[year_month])
)
Result:
Edit:
You don't need a calculated column, you need a measure:
count_of_customer =
COUNTROWS (
SUMMARIZE ( 'table', 'table'[year_month], 'table'[user_id])
)
I'm freshly new to DAX & PowerBI, I tried to create a measure column that will count the number of child of each parent has.
The table is something like this (please understand this table structure might be not ideal, but I can't change the existing).
What I expect is to create a new column that will count how many "child" that the "parent" has based on "Parent ID". Like this.
I've tried using this formula but it returns error
Childcount =
COUNTROWS(
FILTER(
Table,
FILTER(
Table,
Table[Parent ID] <> BLANK()
) = Table[ID]
)
Thank you for your help.
This can be done. Just a quick note, there are two types of calculations you canmake in Power BI: measure and calculated column. I Guess you're in nedd of a calculated column so here's a solution using calculated columns.
First create a calculated column like this:
Parent ID =
var idcol = [id]
var result =
IF(
[Type] = "Child",
CALCULATE(
MAX('Data'[ID]),
FILTER(
ALL('Data'),
'Data'[Type] = "Parent" && 'Data'[ID] < idcol
)
),
BLANK()
)
return
result
Then you can create a second calculated column like this:
Childcount =
var idcol = [ID]
return
CALCULATE(
COUNT(Data[Parent ID]),
FILTER(
ALL(Data),
'Data'[Parent ID] = idcol
)
)+0
Since you're new to Power BI/Dax I would highly recomend you check youtube and sqlbi for free introductory videos. Before you understand the concept of how calculations are performed in Power BI you're gonna have a tough time. But it's worth the time investment!
So, I have the following tables in my Power BI :
Sales : Date | ID_Client | ID_Product | Amount
Client : ID_Client | Name_Client
I would like to get the number of unique BIG clients in any given month. I therefore use the following formula (which I then put in a column in a table with months in rows):
# BIG Clients =
VAR threshold = 10000
RETURN
(
CALCULATE(
DISTINCTCOUNT( Sales[ID_Client] ),
FILTER(
SUMMARIZE(
Sales,
Sales[ID_Client],
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
)
)
QUESTION IS : how can I get the list of those BIG clients for any given month? Let's say I click on the November number of big clients in my table, could another table nearby display the list of those clients ?
Thanks in advance for your kind help, I've been trying for a while :)
I assume that you have a table of clients with the Name column with a one to many relationship with the Sales table and that you do not have duplicate client names. Then you may create a [BIG Sales] measure to be used in a table or matrix visual with client names on the rows.
since [BIG Sales] evaluates to BLANK() for clients with less that threshold sales, they are automatically filtered out from the visual
BIG Sales =
VAR threshold = 10000
VAR BigCustomers =
FILTER(
ADDCOLUMNS(
VALUES( Clients[Name] ),
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
RETURN
SUMX(
BigCustomers,
[Sales]
)
You could create a table or matrix visual with the client on the rows and use your measure in the values field. This will show 1 for all big clients and return blank for the rest (which should hide them). If you don't want to show the measure, you can set the value is 1 in the filter pane and remove the measure from the values field.
A more direct option is to use a simple SalesAmount = SUM ( Sales[Amount] ) measure in the values field and filter like this
In DAX - what is the most efficient way to produce the desired output:
I want to say this would be similar to a correlated subquery in SQL.
OPTION-1
Step-1: Create a Custom Column as below-
is_min_date =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_date = store[Order Date]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_date_current_custommer_id =
CALCULATE(
MIN(store[Order Date]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_date = min_date_current_custommer_id, 1,0)
Step-2: Now add a basic filter in your visual as below and you will get your desired rows your table visual-
OPTION 2:
The same can be achieved using Measure as well instead of creating a Custom Column. Just do this below-
Step-1: Create the measure as below-
is_min_date_measure =
VAR min_order_date_for_current_customer =
CALCULATE(
MIN(store[Order Date]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date]) = min_order_date_for_current_customer, 1,0)
Step-2: Now add visual level filter as below and you will get you desired output-
I'm trying to establish a habit score which indicates +1 for every calendar month with an action and -1 for every calendar month with no action by user. The tricky part to this is I would like to do this per user starting after an indicated date (different for every user). If an action is done twice in the same month, I only want it counted as +1. The below image will help illustrate the logic and desired output.
I need like the result to appear as a calculated column in a separate table with distinct users (this table is already created, I just need the habit score added).
This is probably not the smoothest solution but it seems to work on the testdata you've provided.
N.B: I've assumed that there is a calendar dimension table, in my case called 'Calendar'.
First add a calculated column to the 'Activity' table:
Year-Month =
FORMAT([Activity_Date]; "YYYY-MM")
Then add the desired calculated column to the 'User' table:
Habit =
var minDate =
CALCULATE(
MIN('Activity'[Activity_Date]);
'Activity'[Indicator] <> BLANK()
)
var maxDate =
CALCULATE(
MAX('Activity'[Activity_Date])
)
var calendarActivity = DATEDIFF(minDate; maxDate; MONTH)
var sumAddOne =
CALCULATE(
DISTINCTCOUNT('Activity'[Year-Month]);
'Calendar'[Date] >= minDate;
'Calendar'[Date] <= maxDate
)-1
var sumSubtractOne = calendarActivity-sumAddOne
return
IF(
minDate <> BLANK();
sumAddOne-sumSubtractOne;
BLANK()
)
Then I end up with a 'User' table lilke this: