Compare Selected quarter to last quarter in DAX, even when i select Q1 it should compare with last year's Q4 - powerbi

Hi i am working on a solution in DAX where i have to find customers not ordered in current Quarter compared to its last quarter, i am able to compare Q2, Q3 and Q4 but when i select Q1 i get no value. if i select it should compare with Q4 of last year
how this can be achieved?
Thanks
this was my original question
get new customers compared to last month in dax power bi
with a little bit of tinkering i am here now
Customers Not ordered This Quarter =
VAR ThisQuarter =
SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastQuarter = ThisQuarter - 1
VAR CustomersThisQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
RETURN
CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)
it should compare with the last year's Q4 when i select Q1 in current year

You really need a date table. I have written the code below blind but it should work.
Customers Not ordered This Quarter =
VAR ThisQuarter =
SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastQuarter = IF(ThisQuarter = 1,4,ThisQuarter - 1)
VAR LastYear = IF(ThisQuarter = 1,ThisYEAR -1,ThisYEAR)
VAR CustomersThisQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = LastYEAR),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
RETURN
CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)

Related

Trying to get list of Venues with respect to Suppliers

Hi guys so i am trying to build a report where i have to show new customers this month compared to last month. I am able to calculate new customers fine but i have to show them in a Matrix in Power Bi. This is the Code i am using
New Customers This Month =
VAR ThisMonth =
SELECTEDVALUE( DailyReport[DateCreated].[MonthNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastMonth = ThisMonth - 1
VAR CustomersThisMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = ThisMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = LastMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersThisMonth, CustomersLastMonth )
RETURN
CONCATENATEX( T1, [C1], ", " )
I am getting result like this, two columns one for supplier and one for new venues this month
getting result like this
But expected result should be like this, you know like a list
expected result
i think it may be causing due to how i am concatening it in the last line in code
and please let me know if you need any more information

DAX summarize with measures for This YR Last YR

I am using DAX SUMMARIZE() as shown in this YT video. It works great to summarize measurement for a Dimension table with relationship to Fact table. But when I try to introduce a filtered measure for "This Year" and "Last Year" columns, the calculation ignores the summarized columns and calculates the entire year.
How can I write the query to add "This Year" and "Last Year" columns that respect the summarized values.
DAX query (#1) >
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#"),
"Vol_ThisYr", FORMAT(vol_CurrentYear, "#,#"),
"Vol_PriorYr", FORMAT(vol_PriorYear, "#,#")
)
RETURN
FILTER(
ListTotal, VolumeTotal > 0
)
DAX query (#2) >
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#")
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, VolumeTotal > 0
),
"Vol_ThisYr", FORMAT(vol_CurrentYear, "#,#"),
"Vol_PriorYr", FORMAT(vol_PriorYear, "#,#")
)
Relationships >
Result >
with "VAR vol_CurrentYear" you always get the same value. To put calculations into the row context use CALCULATE() in the ADDCOLUMNS(). You can create measures instead of variables and use them then in ADDCOLUMNS it will have the same effect.
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
--VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
--VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#")
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, VolumeTotal > 0
),
"Vol_ThisYr", FORMAT(CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) ), "#,#"),
"Vol_PriorYr", FORMAT(CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) ), "#,#")
)
The answer from Mik got me down the correct path.
To put calculations into the row context use CALCULATE() in the ADDCOLUMNS(). You can create measures instead of variables and use them then in ADDCOLUMNS it will have the same effect.
I noticed that the "variables" weren't working at all. Best as I can tell in PBI Desktop the DEFINE syntax is invalid, so I couldn't define measures, I had to add the measure definition within the query.
Disposal Attributes (calc field) =
// VAR VolumeTotal = SUM( 'Piped Trucked Volume'[volume_bbls] )
// VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
// VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", SUM( 'Piped Trucked Volume'[volume_bbls] )
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, [Volume] > 0
),
"Vol_ThisYr", FORMAT(CALCULATE(SUM( 'Piped Trucked Volume'[volume_bbls] ), FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) ), "#,#"),
"Vol_PriorYr", FORMAT(CALCULATE(SUM( 'Piped Trucked Volume'[volume_bbls] ), FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) ), "#,#"),
"Volume (,)", FORMAT( [Volume], "#,#")
)
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

Create column "after" in Powerbi (DAX)

I have the following information and I want to create the column "Later" From isProm : is the next day have the same value or no?
Date isProm Later
2018-06-06 1 1
2018-06-13 1 1
2018-08-20 1 1
2018-09-12 1 0
2018-09-12 0 0
Could you help me to do that with day please?
Thank you very much,
Ana
Create your new Custom Column with this below code-
Option 1:
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date =
CALCULATE(
MIN(your_table_name[Date]),
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
)
)
var nex_date_isporm =
CALCULATE(
MIN(your_table_name[isProm]),
FILTER(
ALL(your_table_name),
your_table_name[Date] = next_date
)
)
RETURN IF(current_row_isporm = nex_date_isporm,1,0)
Option 2: You can also use this below code for same output-
later =
var current_row_isporm = your_table_name[isProm]
var current_row_date = your_table_name[Date]
var next_date_isporm =
CALCULATE(
MINX(
TOPN(
1,
FILTER(
ALL(your_table_name),
your_table_name[Date] > current_row_date
),
your_table_name[Date].[Date],ASC
),
your_table_name[isProm]
)
)
RETURN IF(current_row_isporm = next_date_isporm,1,0)
Here is the output. I have slightly different output because of date format in my laptop.

DAX: Mapping items if appeared both in 2 dates

How can I map or count new items that were also in my data the previous month?
For example:
ID / date / item
123 / 01.12.20 / one
123 / 01.11.20 / one
143 / 01.11.20 / two
153 / 01.12.20 / three
Will get:
123 / one
It looks like the Item is the description of the ID.
We can write a measure that shows the Item if it was also present in the preious month or blank otherwise. This way, creating a Table visual in Power BI with the ID and this measure we will just see the IDs and Items that where present in the previous month.
Item if in previous month =
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] ),
VAR currentMonth =
MONTH(
MAX( T[Date] )
)
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = CurrentMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)
This works if we have two months of data, like in your question. If we have more than two months, then maybe that what we really want is to check the previous month of the last one.
Item if in previous to last month =
VAR LastMonth =
MONTH(
MAXX(
ALL( T ),
T[Date]
)
)
RETURN
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] )
&& MONTH(
MAX( T[Date] )
) = LastMonth,
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = LastMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)