Group Items in Comma Separated values in Dax - powerbi

I have following DAX code. I need comma separated holiday list based on Week No . I tired with CONCATENATEX function and it combine all comma separated holiday list in each row instead of week wise. Any one have any idea how we can achieve below expected using DAX.
Dax Code:
DEFINE
VAR A = UNION( ROW ("WeekNo", 1, "USAHoliday", "New Year", "CANHoliday","New Year")
,ROW ("WeekNo", 16, "USAHoliday", "Easter Sunday", "CANHoliday","Easter Sunday")
,ROW ("WeekNo", 16, "USAHoliday", "", "CANHoliday","Easter Monday")
,ROW ("WeekNo", 27, "USAHoliday", "Independence Day", "CANHoliday","")
,ROW ("WeekNo", 28, "USAHoliday", "", "CANHoliday","Independence Day")
,ROW ("WeekNo", 22, "USAHoliday", "Memorial Day", "CANHoliday","")
)
VAR AHoliday = DISTINCT(FILTER( UNION(SELECTCOLUMNS(A,
"WeekNo",[WeekNo]
,"Holiday",[USAHoliday]
),
SELECTCOLUMNS(A,
"WeekNo",[WeekNo]
,"Holiday",[CANHoliday]
)
),[Holiday]<>""))
VAR HolidayList=SUMMARIZE(AHoliday
,[WeekNo]
,"HolidayList.", CONCATENATEX(AHoliday,[Holiday],",", [Holiday],ASC)
)
EVALUATE HolidayList
Expected Result

HolidayList should be defined as, for example:
SUMMARIZE(
AHoliday,
[WeekNo],
"HolidayList",
CONCATENATEX(
FILTER( AHoliday, [WeekNo] = EARLIER( [WeekNo] ) ),
[Holiday],
", "
)
)

Related

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.

DAX Measure - Extrapolate last Date for COUNT

I have a table that contains 3 columns:
Order
Date
State
Each row / record shows if the state was changed:
Now I would like to calculate the number of order that are below state 3 for each date in the calendar.
In the example above you can see there is nor entry for order 100 for 07.01.2022. But for this date the order is still below 3 as you can see in the record before.
How would you do that?
I think this might be better to do in PowerQuery.
Suppose the table mentioned in your question is order_state:
let
Source = Table.FromRows(
{
{100, 1, "1/1/2022"},
{100, 2, "1/5/2022"},
{100, 3, "1/8/2022"},
{101, 1, "1/5/2022"},
{101, 2, "1/6/2022"},
{101, 3, "1/7/2022"},
{102, 1, "1/7/2022"},
{102, 2, "1/9/2022"},
{102, 3, "1/10/2022"}
},
{"Order", "Status", "Date"}
),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
#"Changed Type"
Then I believe you can calculate what you want with a query like this:
let
start_date = List.Min(order_state[Date]),
end_date = List.Max(order_state[Date]),
days = Duration.Days(end_date - start_date),
dates = Table.FromList(List.Dates(start_date,days,#duration(1, 0, 0, 0)), Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
joined = Table.AddColumn(
dates,
"order_state_rows",
(current) => Table.RowCount(
Table.Distinct(
Table.SelectColumns(
Table.SelectRows(
order_state,
(row) => (row[Date] <= current[Date] and row[Status] <> 3)
)
,{"Order"}
)
)
)
)
in
joined
Which gives this result:

Row number in Dax

I have following DAX code. I need auto increment row number where FileNo and CI_No code are same. I tired with RANKX function but it return rank as 1 for all records. Any one have any idea how we can achieve this using DAX.
Thanks in advance.
Dax code:
DEFINE
VAR A = UNION( ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 20, "CI_No", 201, "Amount" ,200)
,ROW ("FileNo", 20, "CI_No", 201, "Amount" ,200)
,ROW ("FileNo", 20, "CI_No", 301, "Amount" ,300))
VAR B= ADDCOLUMNS(A,
"RowNo", RANKX(
FILTER(A,
[FileNo]=EARLIER([FileNo]) && [CI_No]=EARLIER([CI_No])
),
[Amount], , DESC
) )
EVALUATE B
Expected Result
You can try with mix of AddIndexColumn (from TransformData) and new Calculated column:
RowNo =
RANKX(
FILTER( ALLEXCEPT('sample','sample'[RowNo]) , [FileNo] = EARLIER('sample'[FileNo]) && [CL_No] = EARLIER('sample'[CL_No]) ),
[DummySum], , DESC
)
DummySum = sum('sample'[Index])
EDIT:
Table = VAR A = ADDCOLUMNS(UNION( ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 10, "CI_No", 101, "Amount" ,100)
,ROW ("FileNo", 20, "CI_No", 201, "Amount" ,200)
,ROW ("FileNo", 20, "CI_No", 201, "Amount" ,200)
,ROW ("FileNo", 20, "CI_No", 301, "Amount" ,300)), "Index", [Amount]+RAND())
VAR B= ADDCOLUMNS(A,
"RowNo", RANKX(
FILTER(A,
[FileNo]=EARLIER([FileNo]) && [CI_No]=EARLIER([CI_No])
),
[Index], , DESC
) )
return B

Current Fiscal Week with ISO Calendar

I have implemented a fiscal calendar into my Power BI Data Model for some time intelligence magic.
This fiscal calendar has a specific start and end date for each month and is mainly based on ISO 8601 Calendar (Gregorian).
However, my current approach is not working anymore for this year, since the filter "Current Fiscal Week" is not working. If I filter on Current Fiscal Week, then I get the second week of January instead of the first week.
But why?
My code has the following structure:
Calendar =
VAR BaseCalendar =
CALENDAR (
DATE ( 2020, 1, 1 ),
DATE ( 2022, 01, 03 )
)
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR WeekNumber =
WEEKNUM ( BaseDate )
VAR Year =
YEAR ( BaseDate )
VAR FiscalWeek =
WEEKNUM (
BaseDate,
21
)
VAR FiscalYear =
IF (
FiscalWeek < 5
&& WeekNumber > 50,
Year + 1,
IF (
FiscalWeek > 50
&& WeekNumber < 5,
Year - 1,
Year
)
)
RETURN
ROW (
"Fiscal Week", FiscalWeek,
"Current Fiscal Week",
IF (
AND (
FiscalWeek
= WEEKNUM (
TODAY (),
2
),
FiscalYear
= YEAR (
TODAY ()
)
),
TRUE (),
FALSE ()
)
)
)
I think that the Current Fiscal week should be found with the same formulas used to compute the FiscalWeek and FiscalYear. I put the variable definitions for the new variables outside of the GENERATE, since they just depend on TODAY()
VAR TodayBaseDate =
TODAY()
VAR TodayWeekNumber =
WEEKNUM( TodayBaseDate )
VAR TodayYear =
YEAR( TodayBaseDate )
VAR TodayFiscalWeek =
WEEKNUM( TodayBaseDate, 21 )
VAR TodayFiscalYear =
IF(
TodayFiscalWeek < 5
&& TodayWeekNumber > 50,
TodayYear + 1,
IF( TodayFiscalWeek > 50 && TodayWeekNumber < 5, TodayYear - 1, TodayYear )
)
VAR BaseCalendar =
CALENDAR( DATE( 2020, 1, 1 ), DATE( 2022, 01, 03 ) )
RETURN
GENERATE(
BaseCalendar,
VAR BaseDate = [Date]
VAR WeekNumber =
WEEKNUM( BaseDate )
VAR Year =
YEAR( BaseDate )
VAR FiscalWeek =
WEEKNUM( BaseDate, 21 )
VAR FiscalYear =
IF(
FiscalWeek < 5
&& WeekNumber > 50,
Year + 1,
IF( FiscalWeek > 50 && WeekNumber < 5, Year - 1, Year )
)
RETURN
ROW(
"Week day", FORMAT( [Date], "ddd" ),
"Today", TODAY(),
"Fiscal Week", FiscalWeek,
"Current Fiscal Week",
AND( FiscalWeek = TodayFiscalWeek, FiscalYear = TodayFiscalYear )
)
)

Custom matrix header

I need to create a custom header like the picture below:
I check this link Custom aggregate column in power bi matrix
But I don't undestand how to do the same to my case?
Edit
I try to create calculated table but I didn't get the data for dim5 and dim6, how can I modify it?
Edit
Dim_prduit
My problem is how to dispaly Nombre product , and then like hierarchy dim5 then dim6 in the header?
It's ugly but you can write a header table like this and then define a switching measure based on the appropriate indices:
Header =
ADDCOLUMNS (
UNION (
DATATABLE (
"Top", STRING,
"Index1", INTEGER,
"Middle", STRING,
"Index2", INTEGER,
"Bottom", STRING,
"Index3", INTEGER,
{
{ "Nombre product", 1, "", 0, "", 0 },
{ "Affaires nouvelles", 2, "Total", 8, "", 0 },
{ "Affaires nouvelles", 2, "%Total", 9, "", 0 }
}
),
SELECTCOLUMNS (
SUMMARIZECOLUMNS ( Dim_Prod[dim5], Dim_Prod[dim6] ),
"Top", "Affaires nouvelles",
"Index1", 2,
"Middle", Dim_Prod[dim5],
"Index2", RANK.EQ ( Dim_Prod[dim5], Dim_Prod[dim5], ASC ),
"Bottom", Dim_Prod[dim6],
"Index3", RANK.EQ ( Dim_Prod[dim6], Dim_Prod[dim6] )
)
),
"Index0", 100 * [Index1] + 10 * [Index2] + [Index3]
)
Output:
Sample measure:
SampleMeasure =
VAR Top = SELECTEDVALUE ( Header[Top] )
VAR Middle = SELECTEDVALUE ( Header[Middle] )
VAR BottomIndex = SELECTEDVALUE ( Header[Index3] )
RETURN
SWITCH (
TRUE (),
Top = "Nombre product", [NombreProductMeasure],
Top = "Affaires nouvelles" && BottomIndex <> 0, [DimensionMeasure],
Middle = "Total", [TotalMeasure],
Middle = "%Total", [%TotalMeasure]
)
This is pretty hacky though. Power BI may not be the best tool here.
Power BI is not a pixel-perfect data visualization tool, therefore, it is not possible to create customer headers using Built-in visualizations.
Therefore you have pretty much two options:
Build your own custom visualization, using Javascript, Python or R
Use a pixel-perfect tool like SSRS