SubQuery in DAX - powerbi

I am new in Power BI, can anyone help me to convert the below query into DAX
Query:
select YEAR([CloseDate]),MONTH([CloseDate]), sum(days_to_close)/count(distinct id) as 'Number of Days to Close' from
(
select [CloseDate], id, days_to_close from [dbo].[test]
where sname='Closed' group by [CloseDate], id, days_to_close
) a
group by YEAR([CloseDate]),MONTH([CloseDate])
order by 1,2
MyCode:
CALCULATE(
DIVIDE(
SUM(test[days_to_close]),
DISTINCTCOUNT(test[id])
),
test[sname]="Closed",
)

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 - ranking on a date

I tried to make a ranking on a date so that every beginning of a month it resets but I can't get it. the furthest I got is that every month simply gets an cumulative ranking based on RANKX on power bi which includes a month and a year.
Has anyone needed such a ranking and succeeded?
A generalised set-up for a Calculated Column would be along the lines of:
=
VAR ThisMonth =
MONTH( Table1[Date] )
RETURN
RANKX(
FILTER( Table1, MONTH( Table1[Date] ) = ThisMonth ),
Table1[Date],
,
ASC
)

power bi dax, sum up all latest monthly entries

Hi I have a data table in powerbi structured
id date data
1 2022-10-30 123
1 2022-11-01 130
1 2022-11-30 456
the data spans multiple user ids and multiple years and it the values are cumulative (like minutes on a phone plan for instance). This is not the real data
I want to add up the end of month data. In the ideal case, my table would be complete and 2022-10-31 would exist for instance, then I could do
Measure =
CALCULATE(
SUM( 'Table'[data] ),
'Table'[dates] = EOMONTH( 'Table'[dates],0 )
)
This returns 456 but I want 579 (123+456). So i cannot use EOMONTH
I think the answer is some combination of the dax above and
FILTER( Table, Table[date] = MAX( Table[date] ) )
Though if I paste that in solo, it grabs the actual latest date only, not all monthly latest dates
Also I will use slicers on user ID's in case that changes the DAX
Please use this measure to get what you need:
Measure_ =
VAR TblSummary =
ADDCOLUMNS ( YoursTable, "EOM", CALCULATE ( ENDOFMONTH ( YoursTable[date] ) ) )
RETURN
SUMX ( TblSummary, IF ( [EOM] = [date], [data] ) )
If we test our above measure on a table visual:

How to summarize column with condition on power bi?

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.

dcast / melt / reshape a table which has calculated columns in power BI using DAX

I have a table with 5 columns
Store ID | Year | Sales Group 1 | Sales Group 2 | Sales Group 3
All fields with Sales Group are calculated using DAX. I would like to create a new table which contains Store ID, Year, Sales Group and Sales Value.
So essentially I would have 3 rows of data for each store ID and Year, each containing sales value for a different sales group
I want a DAX query to convert from Table 1 to Table 2
Table 1:
Table 2:
It would probably be better to do this in Power Query M, which has a built in unpivot transform, but you can get there with DAX:
UnpivotedTable =
// GROUPBY gives you unique combinations of the columns referenced
VAR StoreYears =
GROUPBY (
'Table',
'Table'[Store ID],
'Table'[Year]
)
RETURN
// UNION does what it says on the label, unions multiple tables
UNION (
// ADDCOLUMNS is also self-descriptive - takes a table, adds columns to it
ADDCOLUMNS (
StoreYears,
// After the table arg, ADDCOLUMNS takes pairs of quoted column name and
// DAX expression to evaluate for the value in that column. We create
// two columns, the group and the sum of the source column for that group.
"Sales Group", 1,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 1] ) )
),
// repeat the pattern above per group
ADDCOLUMNS (
StoreYears,
"Sales Group", 2,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 2] ) )
),
ADDCOLUMNS (
StoreYears,
"Sales Group", 3,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 3] ) )
)
)
Best way to do this is in the Query Editor (Performance). Load your data, go to query editor and select the table.
Select the 3 columns of the sales group, go to Transform -> unpivot
Rename the column headers
End result: