Step1:
I have created a calculated table containing Level, Location, L2code from level sales data. I need to create a report that will count rows based on the level1 group.
Note that there are more levels in the table. This is only an example of one of the levels.
How can I count the rows for each level1?
Step2:
I need to create all combinations of counts based on location and l2 code and count the numbers.
like in example 2 location and 8 distinct l2code so it should be 2*8 =16 total possible rows.
How can I achieve this using the DAX measure?
Source data file
Output report
first one is a simple measure as below-
DistinctRowCount = Count(your_table_name[Level1])
Second one is bit tricky and you can try this below measure-
CrossCount =
var distinct_location =
calculate(
distinctcount(your_table_name[Location]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location]
)
)
var distinct_l2code =
calculate(
distinctcount(your_table_name[l2code]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location],
your_table_name[l2code]
)
)
return distinct_location * distinct_l2code
Sample output-
Related
I use DAX to add two new columns "netWeight_Shipped" and "NetWeight_notShipped" based on existing column "NetWeight_Total" and groupped by "BatchNo" and filtered by OutStockTransactionID as below:
--New column
NetWeight_Shipped =
CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] <> 0
)
--New column
NetWeight_notShipped =
CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] = 0
)
Then put those columns on table as the screenshot. However, two new columns not showing total values in table.
What should I change to have total values for those new columns?
In order to display total values in the table, you should create and use two new measures "netWeight_Shipped" and "NetWeight_notShipped" based on the existing columns, but not two new columns.
-- New measure
NetWeight_Shipped = CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] <> 0
)
-- New measure
NetWeight_notShipped = CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] = 0
)
You should apply aggregation to your column to see the total value below. Look at this image I have added the same column twice and for the first one, there is no total value. But for the second one, there is a total. This is only for the aggregation I applied to the second column but not to the first column-
I have two tables as follow:
the tables are linked to each other using the SRGT key,
I want to calculate the average values for each RESPONDENT_SRGT.
Explanation:
the screenshot below represents the inner join of the two tables:
The needed Results:
I just want the average as a mesure so I can calculate the count of RESPONDENT that have an average greater than or equal to 4, for this example, I will get 2 RESPONDENTS.
I was Able to achieve the desired results in SQL server with the bellow query:
(SELECT COUNT(*) FROM
(SELECT AVG(Cast(DIM.VALUE as Float)) AS AVR
FROM [dbo].[FACT_TABLE] FACT
INNER JOIN [dbo].[DIM_ANSWER_TABLE] DIM
ON FACT.ANSWER_SRGT = DIM.ANSWER_SRGT
GROUP BY FACT.RESPONDENT_SRGT) QRB
WHERE QRB.AVR >= 4 )
It can be like this:
myMeasure =
VAR myTbl=
SUMMARIZE(
'FACT_TABLE'
,FACT_TABLE[RESPONDENT_SRGT]
)
VAR myTblWithAvr=
ADDCOLUMNS(
myTbl
,"avr",CALCULATE(AVERAGE(DIM_ANSWER_TABLE[VALUE]))
)
VAR filtered =
FILTER(
myTblWithAvr
,[avr]>=4
)
RETURN
COUNTROWS(filtered)
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])
)
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-
I know how to do cumulative sum if dataset has Dates, But I am struggling to do same if I do not have dates in my dataset.
Below is the data, I want CUM Sales
I've selected New quick measure -> Totals -> Running total and creates this:
sales running total in part =
CALCULATE(
SUM('Query1'[sales]);
FILTER(
ALLSELECTED('Query1'[part]);
ISONORAFTER('Query1'[part]; MAX('Query1'[part]); DESC)
)
)
Returns:
You can also create own measure to calculate cumulative sum.
Select New Measure from the ribbon and write the following expression
Cumm Sales =
VAR Current_Part = MAX(Test[Part])
RETURN
CALCULATE(
SUM(Test[Sales]),
Test[Part]<=Current_Part,
ALL(Test[Part])
)
Output: