How to sum up a measure based on different levels in Power BI using DAX - powerbi

I have the following table structure:
| Name 1 | Name 2 | Month | Count 1 | Count 2 | SumCount |
|--------|--------|--------|---------|---------|----------|
| A | E | 1 | 5 | 3 | 8 |
| A | E | 2 | 1 | 6 | 7 |
| A | F | 3 | 3 | 4 | 7 |
Now I calculate the following with a DAX measure.
Measure = (sum(Table[Count 2] - sum(Table[Count 1])) * sum(Table[SumCount])
I can't use a column because then the formula is applied before excluding a layer (eg. month). Added to my table structure and excluded month it would look like that:
| Name 1 | Name 2 | Count 1 | Count 2 | SumCount | Measure |
|--------|--------|---------|---------|----------|---------|
| A | E | 6 | 9 | 15 | 45 |
| A | F | 3 | 4 | 7 | 7 |
I added a table to the view which only displays Name 1in which case the measure of course will sum up Count 1, Count 2 and SumCount and applies the measure which leads to the following result:
| Name 1 | Measure |
|--------|---------|
| A | 88 |
But the desired result should be
| Name 1 | Measure |
|--------|---------|
| A | 52 |
which is the sum of Measure.
So basically I want to have the calculation on my base level Measure = (sum(Table[Count 1] - sum(Table[Count 2])) * sum(Table[SumCount]) but when drilling up and grouping those names it should only perform a sum.

An iterator function like SUMX is what you want here since you are trying to sum row by row rather than aggregating first.
Measure = SUMX ( Table, ( Table[Count 2] - Table[Count 1] ) * Table[SumCount] )
Any filters you have will be applied to the first argument, Table, and it will only sum the corresponding rows.
Edit:
If I'm understanding correctly, you want to aggregate over Month before taking the difference and product. One way to do this is by summarizing (excluding Month) before using SUMX like this:
Measure =
VAR Summary =
SUMMARIZE (
Table,
Table[Name 1],
Table[Name 2],
"Count1Sum", SUM ( Table[Count 1] ),
"Count2Sum", SUM ( Table[Count 2] ),
"SumCountSum", SUM ( Table[SumCount] )
)
RETURN
SUMX ( Summary, ( [Count2Sum] - [Count1Sum] ) * [SumCountSum] )

You don't want measure in this case, rather you need new column,
Same formula but new column will give your desired result.
Column = ('Table (2)'[Count1]-'Table (2)'[Count2])*'Table (2)'[SumCount]

Related

What is the more efficient way to find row-wise sum in power bi DAX?

I have a sample table with the following values:
location | col1 | col2 | col3 | col4
------------------------------------------
usa1 | 1 | 1 | 1 | 1
usa2 | 1 | 0 | 1 | 1
values are boolean for true (1) and false (0).
I would like to add a new column that shows the sum per row. from https://www.c-sharpcorner.com/article/sum-multiple-column-using-dax-in-power-bi/
it suggested the following approach:
Measure Total = SUM(table[col1]) + SUM(table[col2]) + ... + SUM(table[colx])
I am getting the expected sum for the four columns I tried. But if I have 20 columns, I was hoping you can guide me to write the DAX more efficiently.
expected output
location | col1 | col2 | col3 | col4 | sum
------------------------------------------
usa1 | 1 | 1 | 1 | 1 | 4
usa2 | 1 | 0 | 1 | 1 | 3
I would use unpivoting feature of PowerQuery to go from wide to long by selecting location and unpivot all other columns.
Then the sum by location would be immediate in any visual, no need for DAX.
One way I do it is
Sum = table[col1] + table[col2] + table[col3] + ...
I am not sure if there is another way for your situation since I only had at most 5 columns to add.

Get count and list of cutomers (ID) that have no sales in the last 2 months

So, I've 2 tables as under
SALES table:
+----+------------+
| ID | SALE_DATE |
+----+------------+
| 1 | 09-21-2021 |
| 2 | 09-21-2021 |
| 3 | 09-21-2021 |
| 2 | 09-21-2021 |
| 3 | 09-21-2021 |
| 1 | 09-21-2021 |
| 5 | 07-22-2021 |
| 6 | 09-21-2021 |
| 9 | 09-21-2021 |
| 7 | 08-21-2021 |
| 8 | 05-21-2021 |
+----+------------+
CUSTOMER Table
+----+
| ID |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+----+
I want to create 2 measures:
1st would be the count of customers that have no sales in the last 2 months, so in this case it would be 2 (8 and 10)
and second measure would give the list of all those customer ID's (8 and 10)
Right now I use this measure to get the list of all ID's that have no sales in last 2 months
show_hide =
VAR current_name = MIN(SALES[ID])
VAR chk_not_in =
IF(
COUNTROWS(
FILTER(
ALL(SALES),
SALES[ID]= current_name && SALES[SALE_DATE])> DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
)
)= 0,
0,
1
)
VAR chk_in =
IF(
COUNTROWS(
FILTER(
ALL(CUSTOMER),
CUSTOMER[ID] = current_name
)
) = 0,
0,
1
)
RETURN IF(chk_in = 1 && chk_not_in = 1, 1, 0)
So every ID with a show_hide of "0" would be the ones that dont have any sales in the last 2 months
I was wondering if there's an easy way to do it and also, I'm not sure how to get the count of all those ID's
First off - I assume your test data was meant to be 2020 instead of 2021 and the ID in the SALES table is a CUSTOMER ID.
I would address this as a measure and a calculated column.
The measure would calculate the customers that have not sold in the past two months. From your data I think you are missing (4) who has not sold anything - bringing the total customers to three (4, 8, 10).
CustomersWithNoSalesIn2Months =
// Work out what date was 2 months ago
VAR twoMonthsAgo = DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
// Count the total distinct customers in the customer table
VAR totalCustomers = CALCULATE(DISTINCTCOUNT(Customer[ID]))
// Count how many distinct customers did have sales in the past 2 months
VAR customersWithSalesInTheLast2Months = CALCULATE(DISTINCTCOUNT(Sales[ID]), Sales[SALE_DATE] > twoMonthsAgo)
// Subtract the customers who did have sales from the total to get the number of customers that did not have sales
RETURN totalCustomers - customersWithSalesInTheLast2Months
The calculated column would be places on the CUSTOMER table and would count how many sales customers had made in the last 2 months.
SalesMadeInTheLast2Months =
VAR MostRecentSale = CALCULATE(MAX(Sales[SALE_DATE]), FILTER(Sales, Customer[ID] = Sales[ID]))
VAR TwoMonthsAgo = DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
RETURN CALCULATE(COUNTROWS(Sales), FILTER(Sales, Sales[SALE_DATE] > TwoMonthsAgo), FILTER(Sales, Sales[ID] = Customer[ID]))
Now you can filter on the Customers table for BLANK sales or use the counts in any other calculation you need. For example, customers 1,2 & 3 have made the most sales in the past 2 months.

DAX Median of category sums

How to calculate median of category sums? I have sample data:
+----------------+-----------+
| category | sales |
+----------------+-----------+
| a | 1 |
| a | 2 |
| a | 4 |
| b | 1 |
| b | 3 |
| b | 4 |
| c | 1 |
| c | 4 |
| c | 5 |
+----------------+-----------+
+----------------+-----------+
| category | sales_sum |
+----------------+-----------+
| a | 7 |
| b | 8 | <- This median
| c | 10 |
+----------------+-----------+
| median of sums | 8 | <- This is expected results, regardless row context
+----------------+-----------+
I have had little success with this measure. It returns correct results but only for category total. But I want to get 8 for each category.
Median_of_sums :=
MEDIANX (
VALUES ( T[Category] ),
SUM ( T[Sales] )
)
I am not entirely sure what you are looking for, but perhaps using the SUMMARIZE function would do the trick here:
Total =
MEDIANX (
SUMMARIZE (
T,
T[category],
"Sales_Calc", SUM ( T[sales] )
),
[Sales_Calc]
)
The idea is to first summarize the information at a category level initially and then calculating the median for the summarized table. This would give the following results for the attached sample:
a 7
b 8
c 10
Total 8
If you want 8 to be reflected for all categories, you would have to use the ALL function to make sure the category context does not affect the calculation:
Total =
MEDIANX (
SUMMARIZE (
ALL ( T ),
T[category],
"Sales_Calc", SUM ( T[sales] )
),
[Sales_Calc]
)
Hope this helps.

PowerBI - Comparing two similar sets of data (Many to Many)

I am trying to compare to sets of data that are very similar. I have done a bridge relation and used M:M relationship on PowerBI but I am still not getting the result I want.
Here is an example of the data:
Dataset 1
Name | Service | Usage
A | 1 | 10
A | 2 | 20
B | 1 | 10
B | 2 | 10
C | 1 | 20
C | 2 | 10
Dataset 2
Name | Service | Usage
A | 1 | 40
A | 2 | 20
B | 1 | 40
B | 2 | 10
C | 1 | 40
C | 2 | 10
Desired output
Name | Service | Usage 1 | Usage 2
A | 1 | 10 | 40
A | 2 | 20 | 20
B | 1 | 10 | 40
B | 2 | 10 | 10
C | 1 | 20 | 40
C | 2 | 10 | 10
Is this possible in PowerBI?
One approach (as suggested in comments), is to separate the distinct Name and Service values into separate dimension tables, in the query editor:
Names:
= Table.FromList(List.Distinct(List.Combine({#"Dataset 1"[Name], #"Dataset 2"[Name]})),Splitter.SplitByNothing(),{"Name"})
Services:
= Table.FromList(List.Distinct(List.Combine({#"Dataset 1"[Service], #"Dataset 2"[Service]})),Splitter.SplitByNothing(),{"Service"})
Create the DAX measures you want:
Usage 1 = SUM ( 'Dataset 1'[Usage] )
Usage 2 = SUM ( 'Dataset 2'[Usage] )
Now create relationships between the fact tables (Dataset 1, Dataset 2) and the dimension tables (Names, Services):
Then simply layout the visual as required:
Another approach may be to combine your dataset fact tables into one table, with an added "dataset" column:
Create your "combined" table in the query editor.
Combined Table:
= Table.Combine({Table.AddColumn(#"Dataset 1", "Dataset", each "Dataset 1", type text), Table.AddColumn(#"Dataset 2", "Dataset", each "Dataset 2", type text)})
Now use this table as your single source - either with a crosstab visual:
Or by adding separate measure for each dataset:
Usage 1 = CALCULATE ( SUM('Combined Data'[Usage]), 'Combined Data'[Dataset] = "Dataset 1" )
Usage 2 = CALCULATE ( SUM('Combined Data'[Usage]), 'Combined Data'[Dataset] = "Dataset 2" )

Daily Rank Power BI

I have a table with the following headers:
Dates | Category | Value
1/1/00 | A | 100
1/1/00 | B | 200
1/2/00 | A | 300
1/2/00 | B | 100
What I would like to do is to be able to add a custom column with the daily rank as such:
Dates | Category | Value | Rank
1/1/00 | A | 100 | 1
1/1/00 | B | 200 | 2
1/2/00 | A | 300 | 2
1/2/00 | B | 100 | 1
My goal is to run calcs over the top for average rank, etc. How would I write the DAX code for this column?
Cheers
Try this as a calculated column:
Column =
VAR rankValue = 'table'[Value]
RETURN
CALCULATE (
RANK.EQ ( rankValue, 'table'[Value], ASC ),
ALLEXCEPT ( 'table', 'table'[Dates] )
)