Summarizing numbers over Distinct values in DAX Power BI - powerbi

I am having trouble receiving correct result when trying to sum numbers over Distinct values (in DAX Power BI)
I have the following table - Tbl_Eposode:
I expect to have total numbers of [Episode] = 12
But I keep having SUM of [Episode] = 36.
My code just summarizes all Episode values instead only summarizing unique Episodes
(by EpisodeID, ProgramID))
This is my code:
# Pre Homeless Days = CALCULATE(SUM('Tbl_Episode'[Episode]),
ALLEXCEPT('Tbl_Episode','Tbl_Episode'[EpisodeID],
'Tbl_Episode'[ProgramID],
'Tbl_Episode'[ClientID]))
Please Help!

As David Brownse says, this really needs remodelling. However, if you are adamant that this is the way to go then:
# Pre Homeless Days =
SUMX( SUMMARIZE(Tbl_Episode, Tbl_Episode[EpisodeID], Tbl_Episode[ProgramID], Tbl_Episode[ClientID],Tbl_Episode[Episode]), Tbl_Episode[Episode])

You can just group the table in Power Query by EpisodeID, ClientID and ProgramID and compute the maximum of Episode and compute the sumation.
= Table.Group(#"Reordered Columns", {"EpisodeID", "ClientID", "ProgramID"}, {{"Max_Episode", each List.Max([Episode]), type nullable number}})
DAX:
Pre Homeless Days 2 = SUM(Max_Episode[Max_Episode])
Output in Table:
New Table "Max_Episode" created in Fields Pane:

Related

PowerBI DAX Query - undo filter slicer on certain values based on expression

I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.

Get sum of column values for specific rows only | Get sum of sales for each country

I am super new to power bi and Dax and i need to create a calculated column in which i have the total sales for each country respectively.
Here is a screenshot with an oversimplified scenario but if i can do it for this data i can do it in my actual project since the concept is the same.
The column TotalSalesPerCountry is the calculated column.
And here is what the column should look like if it worked. (Colors are just for visual representation)
Basically everywhere you see the same country you should see the same TotalSalesPerCountry values which are calculated by sum-ing the Sales for those countries over the years.
Another DAX function:
= CALCULATE(SUM(CountrySales[Sales]), FILTER(CountrySales, CountrySales[Country] = EARLIER (CountrySales[Country])))
This is simple to do in DAX. Add the column:
TotalSalesPerCountry =
var curCountry = yourTable[Country]
return CALCULATE(SUM(Sales), FILTER(yourTable, curCountry = yourTable[Country]))
Explanation:
For each row in the table, get the country. Filter yourTable on the curCountry and SUM this together).

Linking fields on Power BI

very new to Power, so sorry for asking a silly question.
I've got one table with required skills for a particular role, and a second table with a list of users and self-assessment for each of those skills.
Table1 has a 2 columns [Question] and [Required Score]
Table2 has 3 columns: [User], [Question] and [Score]
I'm trying to write a DAX formula to use as a filter: I want to return only those users where their own score is equal to or greater than the required minimum.
My current Measure is: = Table1[Question] = Table2[Question] && Table1[Required Score] <= Table2[Score]
Ask I'm typing the formula:
the tables are not being recognised
all [fields] are underlined in red
error message "A single value for column 'Question' in table 'Table1' cannot be determined.
Table1[Question] and Table2[Question] Data type = text, Format = Text, Summarization = Don't summarize, Data Category = Uncategorized
Table1[Required Score] and Table2[Score] Data type = Fixed decimal number, Format = Decimal number, Summarization = Don't summarize, Data category = Uncategorized
Can someone please explain what I'm doing wrong and point me in the direction of a fix?
Based on your tables, I'd do the following
In your Relationships, link the Question column, in Table1 & Table2
In DAX, add a new column to Table2; the formula will be something
like: Score >= RELATED(RequiredScore) i.e. a Boolean
In your visual, filter Table2 on this new column = True

Measure (CALCULATE) - two fact tables

Hi DAX Experts around the world,
I hope you can help me with below problem. In short:
there are two fact tables: Table1 and Table2 , not connected between each other and cannot be appended.
they have both relation to Calendar table
I need to create a measure which will return some part from Table1 and a part form Table2, as described on the picture.
When a Nov-21 is selected on the slicer, measure should return Actuals for first 10 months of the year with 100 each month (from Table1) and remaining 2 months with Forecast values from with 200 each month (from Table2).
Is it possible at all as I am out of ideas ?
Thank you in advance.
Max
You can create a copy of the calendar table(Calender (2)), which can be used to create a slicer.
With the help of this table, you can create a measure to calculate the value based on selected date like this:
Amount Measure =
VAR _selected_date = MAX('Calender (2)'[Date])
return SUMX(SUMMARIZE(Calender,Calender[Date]),IF('Calender'[Date]<_selected_date,SUMX(FILTER(Table1,Table1[Version]="Actual"),[Amount]),SUM(Table2[Amount])))

How do I manipulate measure values based on 2 other dimension tables

Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.