DAX Summarize - drops records on group by - powerbi

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

Related

DAX New & Lost Customers Table

I am trying to create a List for New Customers Added during the year and List of Lost Customers, I have written a DAX which works fine in summary count but doesn't work in table matrix.
NTB =
VAR currentCustomers =
VALUES ( Deposits[CIF ID] )
VAR currentDate =
MAX ( Deposits[Source.Date] )
VAR pastCustomers =
CALCULATETABLE (
VALUES ( Deposits[CIF ID] ),
ALL (
Deposits[Source.Date].[Month],
Deposits[Source.Date].[MonthNo],
Deposits[Source.Date].[Year]
),
Deposits[Source.Date] < currentDate
)
VAR newCustomers =
EXCEPT ( currentCustomers, pastCustomers )
RETURN
COUNTROWS ( newCustomers )
Total Row is correct, even if I remove one function the table remains same..
Appreciate your help
try this :
Modelling --> Add Table
Table =
VAR _max =
MAX ( Deposits[Source.Date] )
RETURN
ADDCOLUMNS (
SUMMARIZE ( Deposits, Deposits[CIF ID] ),
"NTB",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] >= _max
)
),
"Lost Customers",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] < _max
)
)
)

Count unique occurrences within a year

My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)

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]))

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)))

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.