DAX summarize with measures for This YR Last YR - powerbi

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 >

Related

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

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)

DAX Summarize - drops records on group by

Source Data:
DAX:
Total Derived =
VAR selected_min_date =
MIN ( 'Date'[Date] )
VAR selected_max_date =
MAX ( 'Date'[Date] )
VAR vTable =
SUMMARIZE (
FILTER (
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
MyTable[DerTax]
)
var result = sumx(vTable, MyTable[DerTax])
RETURN
Result
I was expecting result as 2644.48, but is outputting 1322.24
What am doing wrong.
I was same result same as with below sql.
Select GCode,
sum(DerTax)
from MyTable
where PDate >= #datemin and PDate <=#datemax
group by GCode
I don't think you've grasped how SUMMARIZE works. Since your Gcode and DerTax entries are identical for each of the two rows, your SUMMARIZE statement will generate a table comprising just a single row, viz:
Gcode
DerTax
Grp01
1322.24
Instead of passing DerTax as a GroupBy column, you should be passing an aggregation of that field:
Total Derived: =
VAR selected_min_date =
MIN( 'Date'[Date] )
VAR selected_max_date =
MAX( 'Date'[Date] )
VAR vTable =
SUMMARIZE(
FILTER(
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
"Tax", SUM( MyTable[DerTax] )
)
VAR result =
SUMX(
vTable,
[Tax]
)
RETURN
Result

Power BI Dax - Trying to break circular dependency after one attempt

I am trying to figure out ancestor bloodline in data I have. I feel like there is something I am missing to make this work. This data wouldn't change so not sure if I am missing something I can write in the power query editor when loading / refreshing the data.
While technically it is circular in fields it never going to return the same row it is currently in and the first generation is hard number making a starting point. I only want to reference the mother and father to calculate the child's bloodline. The first generation is a basic IF() statement. Below is as far as I can get before hitting the circular dependency error. I have tried a few things to break it thinking its going to loop.
Logic is:
Each blood is 100% for 1st generations based on their birthplace then it is ((mother blood + father blood) / 2) for each generation after that. I found I can use PATHITEM() to isolate the type of blood but errors with a circular dependency. (This is where I can't figure out how to reference the mother / father to do the calculation.) If I take this part out I get the image below working for 1st generation and correct mother / father for second generation.
Asisa Blood =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR current_blood = 'Sheet1'[Birthplace]
VAR current_mother_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Mother's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR current_father_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Father's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR gen1_value = 100
RETURN
IF(AND(LOWER(current_gen) = "1",LOWER(current_blood) = "asisa"),
gen1_value,
((current_mother_blood + current_father_blood)/2)
)
Blood Mix concatenates the four blood types into one field for easy look up in next step.
Blood mix =
VAR current__id = 'Sheet1'[ID]
VAR current_blood_a = 'Sheet1'[Asisa Blood]
VAR current_blood_b = 'Sheet1'[Africa Blood]
VAR current_blood_c = 'Sheet1'[Europe Blood]
VAR current_blood_d = 'Sheet1'[North America Blood]
RETURN
current_blood_a & "|" & current_blood_b & "|" & current_blood_c & "|" & current_blood_d
Mother and Father are lookups on blood mix with mother or father ids
Mother's Blood Mix =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR gen_value = 'Sheet1'[Blood mix]
VAR current_parent_id =
IF(LOWER(current_gen) = "1",current_id,'Sheet1'[Mother ID])
VAR result =
CALCULATE(
DISTINCT('Sheet1'[Blood mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_parent_id
),
REMOVEFILTERS('Sheet1')
)
RETURN
result
You can try to do this way (rather high resource consuming):
NEW COLUMN:
heritage_Father = PATH(Blood[ID],(Blood[FatherID]))
heritage_Mother = PATH(Blood[ID],(Blood[MotherID]))
Mancestor = LOOKUPVALUE(Blood[heritage_Mother],Blood[ID],Blood[FatherID]) &"|"& Blood[heritage_Mother]
Fancestor = LOOKUPVALUE(Blood[heritage_Father],Blood[ID],Blood[MotherID])&"|"&Blood[heritage_Father]
NEW MEASURE:
ASIA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AsiaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))
AFRICA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AfricaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))

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 measure to correctly calculate previous week category and subtotal

I am using a matrix table in powerbi to show previous week totals for different areas (categories). I have the majority of it working but I am not able to correctly get the subtotals on the table working.
I believe it has to do with the filtering that I am using - i have been unable to correct it.
screen capture
As you can see my Total for week 24 previous is missing
Dax code is:
VAR Area =
MAX ( 'SumTable'[Area Name] )
VAR CurrentWeek =
SELECTEDVALUE ( SumTable[WeekofYear] )
VAR CurrentYear =
SELECTEDVALUE ( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), ALL ( SumTable ) )
RETURN
IF (
HASONEVALUE ( SumTable[Area Name] ),
SUMX (
FILTER (
ALL ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
&& SumTable[Area Name] = Area,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
&& SumTable[Area Name] = Area
)
),
SumTable[BOE]
),
SUMX (
FILTER (
ALLSELECTED ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
)
),
SumTable[BOE]
)
)
Data Table:
Example Table Format
Thank you, first time poster!
B
I would start by spliting my data table from my date table.
And I guess you don't need to ALL the whole table, just the columns for year and weeknumber and keep the Area in context, that way you don't have to bother if HASONEVALUE it will just work.
SELECTEDVALUE only returns if only a single value for that column is in context, not the case for totals and subtotals.
MyMeasure =
VAR CurrentWeek =
MAX( SumTable[WeekofYear] )
VAR CurrentYear =
MAX( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), SumTable[Year] = CurrentYear-1 )
RETURN
IF(
CurrentWeek = 1,
CALCULATE(
SUM(SumTable[BOE]),
FILTER (
ALL ( SumTable[Year],SumTable[WeekofYear]),
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
)
),
CALCULATE (
SUM ( SumTable[BOE] ),
FILTER (
ALL ( SumTable[WeekofYear] ),
SumTable[WeekofYear] = CurrentWeek-1
)
)
)
I did not have a chance to confirm this code.