How to summarize column with condition on power bi? - powerbi

I'm trying to create table summary table with following conditions
From the Original table to summary table we have to create using the following conditions
1) select distinct ids
2) select screen name base on highest count group by id and today date
3) If two screens are same value on today date with the same id then pick the first screen

This yields the desired result as a calculated table.
SummaryTable =
ADDCOLUMNS(
ADDCOLUMNS(
FILTER(
SUMMARIZE(
OriginalTable,
OriginalTable[ID],
OriginalTable[StartDate]
),
OriginalTable[StartDate] = TODAY()
),
"Count", CALCULATE( MAX( OriginalTable[Count] ) )
),
"Screen",
VAR CurrentCount = [Count]
RETURN CALCULATE( MIN(OriginalTable[Screen]), OriginalTable[Count] = CurrentCount )
)
Output:

You could create a Rank calculation using the following formula:
Rank = IF(Original[Start Date]=TODAY(),RANKX(CALCULATETABLE(Original,ALLEXCEPT(Original,Original[ID])),Original[Count]),0)
Output:
You should replace "Original" with your table name in the calculation. Once the Rank is created, you can just filter for Rank=1 and you should have the desired result. Hope this helps.

Related

DAX RANKX Within Group

I have a table called CarSales with the following sample data:
I already have a measure that ranks all Brands by sales:
Rank = RANKX(ALL('CarSales'[Brand]),[NetSales])
However, I need another measure/column **"Rank(Country)" ** which ranks the Brands within the Country group (without having to display the Country group in the table)
Thanks
Try the following measure:
=
VAR ThisCountry =
MIN( CarSales[Country] )
RETURN
RANKX(
FILTER( ALLSELECTED( CarSales ), 'CarSales'[Country] = ThisCountry ),
[NetSales]
)

Power bi DAX calculate the average of values over a group by 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)

DAX Using Countrows and Filter to compare value in the same table

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!

DAX Filtering Logic For Each Value of a Dimension

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-

Power BI Slicer should not filter complete table

I have a below scenario in which i have a location slicer which filters the table perfectly.
Current Situation
What i required is when location is selected in slicer it should show All location but filtered values as 0.
Result
How can i achieve this in power BI
First create a new table Location with all distinct locations like the following:
And create a relationship like this:
Use the Location column from the Location table to create the slicer.
Finally use the following dax function to create a measure:
Measure =
VAR __location = SELECTEDVALUE( 'Location Table'[Location] )
VAR __flag = COUNTROWS( 'Location Table' )
Return
IF(
__flag > 1,
SUM( 'Table'[Values] ),
IF(
SELECTEDVALUE( 'Table'[Location] ) = __location,
SUM( 'Table'[Values] ),
0
)
)
This is the expected result: