Most repeated value in a Card visual using Power BI - powerbi

I'm trying to return the most repeated ID in a table, inide a Card visual in Power BI.
I've tried the following:
test =
FIRSTNONBLANK(
TOPN(
1,
VALUES(
Sales[Customer ID]
),
RANKX(
ALL(
Sales[Customer ID]
),
COUNTX(
Sales,
Sales[Customer ID]
),,
ASC
)
),
1
)
This measure returns the following:
Card Visual
However, this is not the most repeated value. When I add an ID count in a table, I see this:
ID count table
When I order by the ID count, it tells me that the most repeated ID is WB-21850.
Table showing the right ID
I don't know what I'm missing here...

Most Repeated Customer ID =
var t = ADDCOLUMNS(
SUMMARIZE(Sales, Sales[Customer ID]),
"#qty", CALCULATE(COUNTROWS(Sales))
)
var rtn = MAXX(TOPN(1, t, [#qty], DESC), Sales[Customer ID])
return rtn

Related

Power BI - Summarize table by 2 different date column

I have a table with information about our projects. I want to create a graphic showing number of projects started and finished each month. The original table looks like this:
Project ID
Start Date
Finish Date
1
02/10/2021
08/19/2021
2
02/15/2021
06/22/2021
3
05/08/2021
08/12/2021
4
06/25/2021
08/16/2021
5
06/29/2021
11/30/2021
6
08/12/2021
12/05/2021
The result table should look like this:
Month
Started
Finished
02/2021
2
0
05/2021
1
0
06/2021
2
1
08/2021
1
3
11/2021
0
1
12/2021
0
1
I've tried to use SUMMARIZE, but I haven't found a way to "group" 2 different dates in only 1 column.
Is there a way to do it?
First, create a Calendar Table.
Calendar Table
Calendar =
ADDCOLUMNS (
CALENDAR ( MIN ( 'Table'[Start Date] ), MAX ( 'Table'[Finish Date] ) ),
"Month Year", FORMAT ( [Date], "mm/yyyy" )
)
From there, you can create your DAX calculations
Started
Started =
VAR _SelectedMonth =
SELECTEDVALUE ( 'Calendar'[Month Year] )
VAR _FilterCalendar =
SELECTCOLUMNS (
FILTER ( 'Calendar', [Month Year] = _SelectedMonth ),
"#Date", [Date]
)
VAR _Result =
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Start Date] IN _FilterCalendar )
RETURN
_Result
Finished
Finished =
VAR _SelectedMonth =
SELECTEDVALUE ( 'Calendar'[Month Year] )
VAR _FilterCalendar =
SELECTCOLUMNS (
FILTER ( 'Calendar', [Month Year] = _SelectedMonth ),
"#Date", [Date]
)
VAR _Result =
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Finish Date] IN _FilterCalendar )
RETURN
_Result
Output

Convert SQL query into DAX query for PowerBI visual

I have a table like below and trying to count the number of job_ids that are in "PAUSED" status in databricks and indicate it in PowerBI report.
If there is job id that has both of "PAUSED" and "UNPAUSED" status with the same timestamp, I should count that job_id as well as it's in the "PAUSED" status.
table1
job_id
status
timestamp
A
PAUSED
2022-08-02T21:09:13
A
UNPAUSED
2022-08-02T21:09:13
B
PAUSED
2022-08-10T21:09:15
B
PAUSED
2022-07-20T21:09:13
A
PAUSED
2022-08-12T21:09:13
C
PAUSED
2022-08-01T21:07:19
C
PAUSED
2022-08-01T21:07:19
C
UNPAUSED
2022-08-03T21:07:19
B
UNPAUSED
2022-07-20T21:09:13
A
UNPAUSED
2022-08-04T21:09:13
and this is the sql query I wrote
SELECT count(job_id) FROM (SELECT distinct job_id,status,DENSE_RANK over() partition
by job_id order by timestamp desc AS rn FROM table1) t2 WHERE
t2.rn = 1 and t2.status = "PAUSED"
What I want to do next is to display the number in PowerBI using the card visual.
To do that I think I need to convert the sql query above into DAX query to get the correct number..(not sure if there is better way to perform it, I'm new to PowerBI)
Anyone could help me with this query..?
Would appreciate any kind of help!
In Power BI you would need the following measure:
Paused Jobs =
CALCULATE(
DISTINCTCOUNT('Table'[job_id]),
'Table'[status] = "PAUSED"
)
You can then put this measure onto a Card visual next to your data table
I hope This is what you want :
If we test the summary table:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
SummaryTable
And the resulting screenshot:
You can check the result with sql query :
If It is what you want, and want it as a measure:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
Result

more filter apply on the DAX

Good Day!
Request
The following is my raw data, with some conditions applied to check which columns need to be counted.
Example:
Row 1, direction is export, department code is not starting with 'D',
will count all 6 columns (ETD, ATD,ETA, ATA, Estimated Delivey,
Actual Delivery) , and only 5 have been filled in, so get 83 as the
percentage.
Row 2, direction is export, department code starting
with 'D', will only count 4 columns (ETA, ATA,Estimated Delivery
and Actual Delivery), and only 2 has been filled in, so get 50%.
What I have now:
I have a code but it only shows all columns and which column has been filled, and I would like some help in calculating the conditions as stated above.
DAX: (calculate not blank)
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var tab = {RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]}
var result =
COUNTROWS(
FILTER(
tab,
[Value]<>BLANK()))
return
IF(
ISBLANK(result),
0,
result
) ),[Count])
DAX: Calculate blank
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var tab = {RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]}
var result =
COUNTROWS(
FILTER(
tab,
[Value]=BLANK()))
return
IF(
ISBLANK(result),
0,
result
) ),[Count])
Any help will be greatly appreciated.
Attached here with my pbix: https://drive.google.com/file/d/1KIROrAzNEp710JEfxMfiZLzvTpuHj3OZ/view?usp=sharing
Thank you!
For the count I added the decision making of the "D";
Count =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var Dep = RawData[Dep]
var res1 = COUNTROWS(
FILTER(
{RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
NOT ISBLANK([Value])
)
)
var res2 = COUNTROWS(
FILTER(
{RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
NOT ISBLANK([Value])
)
)
return if (LEFT(Dep, 1) = "D", res1, res2)
),
[Count]
)
I also followed your logic to go with the Counts measure:
Counts =
SUMX(
ADDCOLUMNS(
RawData,
"Count",
var Dep = RawData[Dep]
var res1 = COUNTROWS(
FILTER(
{RawData[ETD],RawData[ATD],RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
ISBLANK([Value])
)
)
var res2 = COUNTROWS(
FILTER(
{RawData[ETA],RawData[ATA],RawData[Estimated Delivery],RawData[Actual Delivery]},
ISBLANK([Value])
)
)
return if (LEFT(Dep, 1) = "D", res1, res2)
),
[Count]
)
Then I added the measure %
% = RawData[Count]/RawData[Total]
Result:
I do believe you would be better of using calculated columns but did not go their as you had choosen measures already.
IF I would have started from scratch, I would have gone with an unpivot table in power query what makes the coding more dynamic

DAX Creating Calendar Table With Additional Field

I'm creating a calendar table in DAX:
Dates =
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
)
The work weeks table contains a week number and a start date for each week.
For each date in my new Dates table, I want to assign a work week number using the start date of the work week. Note that work weeks start on different days of the week (they're assigned properly in my work weeks table though).
So what I'm trying is:
Dates =
ADDCOLUMNS (
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
),
"Work Week",
CALCULATE (
MAX ( 'Work Weeks'[Start Date].[Date] ),
'Work Weeks'[Start Date] <= Date
)
)
I'm not sure how to reference the current row/date in the condition at the end. And then I also need to return the work week number, rather than just the start date.
Assuming your work week table looks something like this:
You can use this date table code to add the week number:
Date Table =
VAR ListOfDate =
VAR MinDate = MIN ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR MaxDate = MAX ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR StartDate = DATE ( 2021, 1, 1 ) -- DATE ( YEAR ( MinDate ), 1, 1 )
VAR EndDate = DATE ( 2021, 12, 31 ) -- DATE ( YEAR ( MaxDate ), 12, 31 )
VAR Result = CALENDAR ( StartDate, EndDate )
RETURN
Result
VAR WorkWeekTable = ALL ( WorkWeek )
VAR CalendarTable =
GENERATE (
ListOfDate,
VAR CurrentDate = [Date]
RETURN
ROW (
"Calendar Year Number", YEAR ( CurrentDate ),
"Calendar Month Number", MONTH ( CurrentDate ),
"Work week",
CALCULATE (
MIN ( WorkWeek[Work Week Number] ),
CurrentDate >= WorkWeek[Work Week Start Date],
CurrentDate <= WorkWeek[Work Week End Date],
REMOVEFILTERS ( )
)
)
)
RETURN
CalendarTable
The result will look like this:

Switching Measure in a line chart with the Slicer selection on Power BI

I have a line chart that shows YTD revenue for the last 3 years and a line for Goal.
I have 3 slicers Region, District, and Branch all coming from the table DIMBRANCH
I have the following goals tables:
goalbyRegion
goalbyBranch
goalbyCompany
Requirement When nothing on the slicer is selected then the goal from table goalbycompany should be displayed.
when a region is selected, show goal on the line chart of the selected region coming in from goalbyregion table.
Problem Currently the revenue changes over slicer selection because the revenue is coming from a single table tbl_revenue.
The image shows the revenue and goal line charts if nothing is selected on the slicer.
DAX currently used:
2017/Revenue =
VAR _year =
YEAR ( TODAY () ) - 2
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2018/Revenue =
VAR _year =
YEAR ( TODAY () ) - 1
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2019/Revenue =
VAR _year =
YEAR ( TODAY () )
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2019/Goals Amount =
VAR _year =
YEAR ( TODAY () )
RETURN
CALCULATE (
SUM ( GoalByCompany[GoalAmount] ),
FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
)
DAX I am trying to use to achive my goal with switch function.
Switch Goal =
var _region = SELECTEDVALUE(Branch[Region])
var _branch = SELECTEDVALUE(Branch[Branch])
var _district = SELECTEDVALUE(Branch[District])
var _year = year(TODAY())
var _goalregion = CALCULATE(
SUM(GoalByRegion[GoalAmount]),
FILTER(GoalByRegion , GoalByRegion[year] = _year))
var _regionselection =
SWITCH(_region,
"RegionGoal",_goalregion
)
return
switch (_regionselection,"Region",_goalregion,blank())
I am not used to the switch function yet. I thought I could use this to get to my requirement.
this is when I select a region.
Here is the relationships between my tables
Additonal tables: revenue table links to branch table with branchID
calendarweek table links to date table via weekendingdate column.
You don't need to use SWITCH, if you're only considering whether Region is filtered or not. You can simply use an IF statement, to decide which measure to calculate:
2019/Goals Amount =
VAR _year =
YEAR ( TODAY() )
RETURN
IF (
HASONEFILTER ( Branch[Region] ),
CALCULATE (
SUM ( GoalByRegion[GoalAmount] ),
FILTER ( GoalByRegion, GoalByRegion[Year] = _year )
),
CALCULATE (
SUM ( GoalByCompany[GoalAmount] ),
FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
)
)