Power BI DAX - Need help in displaying Measure with Values and Text - powerbi

I'm trying to display a measure value with text in Card Visual.  For instance, I'd like to display all IDs with HasWrongTitle = True by Department (Slicer).  Based on the below data, I'd like to show "2 of 4" without slicer selection and "1 of 4" with slicer set to IT or Finance.
The below measure works partially but displays the same value for total as well.
MeasureWrongTitle =
IF(
ISBLANK(
CALCULATE(
DISTINCTCOUNT(Table[ID]),
FILTER(
Table,
Table[HasWrongTitle]="True"
)
)
),
"0 of " &
COUNTROWS(
SUMMARIZE(
Table,
Table[ID]
)
),
CALCULATE(
DISTINCTCOUNT(Table[ID]),
FILTER(
Table,
Table[HasWrongTitle]="True"
)
) & " of " & COUNTROWS(SUMMARIZE(Table,Table[ID]
)
)
Table:
ID Name Department HasDirectReport HasWrongTitle
100 John Doe1 Admin True False
101 John Doe2 IT False True
102 John Doe3 HR True False
103 John Doe4 Finance True True

You can try this below measure with your logic but in simplified way-
MeasureWrongTitle =
var has_wrong_title_count =
CALCULATE(
DISTINCTCOUNT('Table'[ID]),
FILTER(
'Table',
'Table'[HasWrongTitle] = TRUE()
//-- User ""True" if your column is string type
)
)
var total_id = DISTINCTCOUNT('Table'[ID])
RETURN IF(has_wrong_title_count = BLANK(),0,has_wrong_title_count) & " of " & total_id
Here is the output using your data-

Related

Most repeated value in a Card visual using Power BI

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

How to Create a Slicer that uses dates and that only contains YTD Months dynamically by current date in PowerBI

Trying to create a slicer that when clicked only the months YTD, specifically up the the last month of current.
Such as slicer box clicked: (Say current month is August) then Months "Jan","Feb",..."July" would only display to click.
I've tried several things, my last attempt was to create a Dax table with dates and try to do a switch:
Dates =
var CurrentMonthInt = month(TODAY())
var monthis8 = "Jan" + "Feb" + "March" + "April" + "May" + "June" + "July"
VAR BaseTable =
CALENDAR(
DATE( YEAR ( MIN(Reporting[InvoiceDate])),01,01),
DATE( YEAR ( MAX(Reporting[InvoiceDate])),12,31)
)
RETURN
ADDCOLUMNS(
BaseTable,
"Year", YEAR([Date]),
"Month", FORMAT([Date],"mm"),
"Year Month", FORMAT([Date],"YYYY MM"),
"Month YTD", SWITCH(TRUE(),
CurrentMonthInt = 1, "Jan",
CurrentMonthInt = 8, monthis8,
"testing"
)
)
this returns a variant data type and will not work. I am thinking this is trying to add them all up on the same row.
Hello Please test this and let me know if It solves your problem.
Dates =
VAR CurrentMonthInt =
MONTH ( TODAY () )
VAR BaseTable =
CALENDAR (
DATE ( YEAR ( MIN ( Reporting[InvoiceDate] ) ), 01, 01 ),
DATE ( YEAR ( MAX ( Reporting[InvoiceDate] ) ), 12, 31 )
)
RETURN
FILTER (
ADDCOLUMNS (
BaseTable,
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "YYYY MM" )
),
[Month] <= CurrentMonthInt - 1
)
Note: I haven't tested it.

How to get the Top N vales based on a column for each category in PowerBI?

I am facing the issue while filtering out the data based on a "Date" column to fetch top 3 for each category. Below is the sample data:
Can anybody help me with this to get the below-expected output?
You can try this (here dummy data); You can choice ASC or DESC based on your need:
Ranking by Date = var _cat = SELECTEDVALUE( Sheet1[Category])
return
IF(RANKX(FILTER(ALL(Sheet1), Sheet1[Category]= _cat), calculate(MAX(Sheet1[Date])),,ASC ,Skip)<=2, 1,BLANK())
or by Sale:
Ranking by Sales =
IF (
ISINSCOPE ('Sheet1'[Date] ),
VAR ProductsToRank = 2
VAR SalesAmount = [SumOf]
RETURN
IF (
SalesAmount > 0,
VAR VisibleProducts =
CALCULATETABLE (
VALUES ( 'Sheet1' ),
ALLSELECTED ( 'Sheet1'[Date] )
)
VAR Ranking =
RANKX (
VisibleProducts,
[SumOf],
SalesAmount
)
RETURN
IF (
Ranking > 0 && Ranking <= ProductsToRank,
1
)
)
)
Or you can create a new table in DAX like this:
Top2 = GENERATE(VALUES(Sheet1[Category]), TOPN(2, FILTER(SELECTCOLUMNS(ALL(Sheet1[Category], Sheet1[Date]),"Cat",[Category],"Date",[Date]),[Cat] = [Category]),[Date]))

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:

Rolling 20 workday revenue

Currently visualizing sales for the past 30days, but looking to switch it in to the past 20 workdays instead, I've got workday column up and running in datetable, so ideally id want to use a filter of workday=1 and grab the 20 newest rows?
Sales 30d =
CALCULATE([Sales],
FILTER(
ALL(d_dates[date]),
d_dates[date]
>TODAY()-30))
This is what im using to show revenue for past 30 days, what'll i need to change?
You can try with this below measure-
slaes_last_20_days =
VAR today = TODAY()
VAR selected_date_min =
MINX(
TOPN(
20,
FILTER(
ALL(d_dates),
d_dates[date].[Date] <= today
&& workday = 1
),
d_dates[date].[Date],
DESC
),
d_dates[date].[Date]
)
RETURN
CALCULATE(
[Sales],
FILTER(
ALL(d_dates),
d_dates[date].[Date] >= selected_date_min
&& workday = 1
)
)
VAR Last20Workdays =
selectcolumns(
TOPN(
20,
FILTER(
d_dates,
d_dates[date] < TODAY()
&& d_dates[workday] = 1
),
d_dates[date],
DESC
),
"WorkDay",
d_dates[date]
)
This worked.