Out of SummaryTable return values of max [SumaPac] for each IDPlac, I also want to keep date of this max happening, keep only max values in case of tie, keep last newest date out of Date column
Below you’ll find a compact DAX pattern that does all three things you asked for
• aggregates Count_PT per IDPlac / Date
• keeps only the row that has the largest SumaPac per IDPlac
• breaks ties by taking the most-recent Date (DESC)
Copy / paste it into DAX Studio or the “New Table” expression pane and you’ll get one row per IDPlac with the wanted number and the date when that number was hit ⬇️
DAX
EVALUATE
VAR AggPerDay =
-- 1. Sum Count_PT for every (IDPlac, Date) that is in scope
SUMMARIZE (
FILTER (
FACT_ILOSC,
FACT_ILOSC[Date] >= DATE ( 2020, 4, 1 )
&& FACT_ILOSC[Date] <= DATE ( 2021, 2, 28 )
),
FACT_ILOSC[IDPlac],
FACT_ILOSC[Date],
"SumaPac", SUM ( FACT_ILOSC[Count_PT] )
)
VAR Result =
-- 2. One row per IDPlac – TOPN(1 … SumaPac DESC, Date DESC)
-- gives us “largest SumaPac, newest Date”
ADDCOLUMNS (
SUMMARIZE ( AggPerDay, [IDPlac] ),
"SumaPac",
MAXX (
TOPN (
1,
FILTER ( AggPerDay, [IDPlac] = EARLIER ( [IDPlac] ) ),
[SumaPac], DESC,
[Date], DESC -- tie-breaker
),
[SumaPac]
),
"Date",
MAXX (
TOPN (
1,
FILTER ( AggPerDay, [IDPlac] = EARLIER ( [IDPlac] ) ),
[SumaPac], DESC,
[Date], DESC
),
[Date]
)
)
RETURN
Result
What the code does, step-by-step
AggPerDay – exactly your original SUMMARIZE, i.e. one line per IDPlac & Date with the daily SumaPac.
Result – we start with a list of the distinct IDPlac values.
For every IDPlac we run a TOPN(1 …) over the rows that belong to that IDPlac:
• order first by SumaPac (descending)
• then by Date (descending)
– so we end up with the row that has the highest SumaPac and, if several rows share that max, the latest date.
MAXX over that single-row table just returns the two columns we need.
The output looks like this:
IDPlac
SumaPac
Date
101
1 234
2020-11-15
102
2 680
2021-02-02
…
…
…
…and you can of course use the same idea inside a measure if you ever need the result dynamically.