How to use ORDER BY in Power BI DAX? - powerbi

I have a DAX which concatenates values from one column in my data.
I want to rank my data by 2 columns right inside my DAX as its concatenating rows randomly.
How can I achieve that?
My data looks like -
Now, the problem is that I've sorted by id... But I want to sort by "id" and "name" so that it concatenates data in the same manner.
Currently, it's concatenating for one id as a/b/c and for another id as b/a/c
My DAX looks like this -
=
VAR ThisID = 'Table'[id]
RETURN
CONCATENATEX(
FILTER(
'Table',
'Table'[id] = ThisID
),
'Table'[likes],
"/"
)
I tried using 'ORDER BY' with the EVALUATE. But, that's giving ERROR.

Just add the name column as one of CONCATENATEX's orderBy_expression parameters:
=
VAR ThisID = 'Table'[id]
RETURN
CONCATENATEX(
FILTER(
'Table',
'Table'[id] = ThisID
),
'Table'[likes],
"/",
'Table'[name]
)

Related

Power BI how to add a new row based on condition after group

I have a table like this:
enter image description here
I want to create a default value for each group(each month should have a default amount 0), I know we should do group function first, but I do not know want to do next, very appreciate who give me help
I'm not sure exactly where you are going with your request. I understand it as: Adding a row for the first day of each month with a default value of zero.
This is not easy DAX. It's a bit complicated, so feel free to provide more info about your need, there may be another way more simple way.
The first step is to generate a table of the first day of each month. After a quick search, I've found a way in another answer and I was able to modify it a bit.
TABLE2 =
SELECTCOLUMNS(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
"DATA",
[Date],
"AMOUNT",
0
)
What it does:
It generate a calendar between two dates I've arbitrary set : January 1st to Decembre 1st.
It adds a columns in this generated table IsFDOM (Is First Day Of Month) that return True is it is.
This table is then filtered to keep only the rows where IsFDOM is True.
It is wrapped in a SELECTCOLUMNS to keep and rename the columns you want and to match your existing table.
Next, you want to merge your existing table with this newly generated table. The DAX function to do it is UNION and requires both table to have the same format - i.e same number of columns.
TABLENEW =
UNION(
TABLE1,
FILTER(
TABLE2,
NOT TABLE2[DATA] IN VALUES(TABLE1[DATA])
)
)
I've named you existing table TABLE1 in this example.
The FILTER is used to not merge rows (dates) that already exist in TABLE1.
And, to make things even nicer. We can bypass the creation of TABLE2 by embedding it in the table expression of TABLENEW :
TABLENEW =
VAR MaxDate =
MAX(TABLE1[DATA])
VAR TMP_Calendar =
FILTER(
SELECTCOLUMNS(
SUMMARIZE(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
[Date],
"default",
0
),
"DATA",
[Date],
"AMOUNT",
[default]
),
NOT [DATA] IN VALUES(TABLE1[DATA]) &&
[DATA] <= MaxDate
)
RETURN
UNION(
TABLE1,
TMP_Calendar
)
The calendar formerly known as TABLE2 is now created in a variable TMP_Calendar.
To make thing nicer, MaxDate calculate the max date of your existing table and only merge for existing date.

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!

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:

Dax function DATESINPERIOD not giving correct answer

I need to find moving two days sum of sales. I am using DAX function DatesinPeriod but the output is not coming correct. Please help me understand where I am going wrong please. I am using below Dax Formula:
Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
To replicate the scenario first step is to create Dim Date table using - >
Dim Date = GENERATESERIES(date(2019,01,01),date(2019,12,31),1)
second Step is to create DataTable ->
Table = DATATABLE("Date",DATETIME,"Flag1",STRING,"Flag2",STRING,"Sale",INTEGER,{
{"8/1/2019","True","True",200},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"9/3/2019","False","True",60},
{"9/4/2019","False","True",10},
{"9/5/2019","False","True",100},
{"9/6/2019","False","True",30},
{"9/7/2019","False","True",60},
{"9/8/2019","False","False",150},
{"9/9/2019","False","False",80},
{"9/10/2019","False","False",90},
{"9/11/2019","False","False",30},
{"9/12/2019","False","False",20},
{"10/13/2019","False","True",50},
{"10/14/2019","False","True",60},
{"10/15/2019",BLANK(),BLANK(),BLANK()},
{"10/16/2019",BLANK(),BLANK(),BLANK()}
})
3rd Step - create a relation between these tables on date column
4th step - create Measure using - Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
You will see the output coming wrong. see the screenshot. This is very strange. I tried using DatesBetween function , its also giving me the same wrong output.
Use the following measure to obtain the expected result:
SumInRange =
VAR __selectedDate = SELECTEDVALUE( 'Table'[Date] )
VAR __subTable =
FILTER(
ALL( 'Table'[Date] ),
AND(
'Table'[Date] >= __selectedDate -2,
'Table'[Date] <=__selectedDate
)
)
Return
CALCULATE(
SUMX (
DISTINCT ( 'Table'[Date] ),
CALCULATE ( MAX ( 'Table'[Sale] ) )
),
__subTable
)
Be sure to use the Date column from Table instead of the Dim in the visualization.

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.