Filter Table By Summarised Set

m1nkeh

New Member
Joined
Jul 28, 2014
Messages
30
I have a table in my model where the date column is quite unreliable, and i need to find the sum total of 'measure' for when items in this table were first seen...

illustrated with this SQL, here we have two products but i only want to report a measure for when they are in their very first state

Code:
with cte as (    select 1 Id, 'Apple' Product, 'Quote' State, 100 Measure union
    select 2 Id, 'Apple' Product, 'Checking' State, 100 Measure union
    select 3 Id, 'Apple' Product, 'Quote' State, 50 Measure union
    select 4 Id, 'Apple' Product, 'Offered' State, 50 Measure union
    select 5 Id, 'Pear' Product, 'Quote' State, 80 Measure union
    select 6 Id, 'Pear' Product, 'Offered' State, 80 Measure
)


select
    cte.*
from cte
join (
    select
        Product
        ,min(id) FirstId
    from cte
    group by Product
) x
on
    cte.Product = x.Product
where
    cte.Id = x.FirstId;

1EygVot.png


But how can i achieve the same in dax??

I've been messing about for ages with summarize / addcolumns... and i can only get so far as:

Code:
[COLOR=#008080]evaluate[/COLOR]
[COLOR=#0000ff]addcolumns[/COLOR](
    [COLOR=#0000ff]summarize[/COLOR](
        [COLOR=#000000]'FactData'[/COLOR]
        ,[COLOR=#000000]'FactData'[Product][/COLOR]
    )
    ,[COLOR=#ff0000]"MinId"[/COLOR]
    ,[COLOR=#0000ff]calculate[/COLOR](
        [COLOR=#0000ff]min[/COLOR]([COLOR=#000000]'FactData'[Id][/COLOR])
    )
)

so, that quite plainly is the sub query in my T-SQL, but i can't for the life of my figure out how to use it to filter my main table, to only show items in the first state...

i've been trying to use the crossjoin() or generate() functions in order to replicate a cartesian join,

Code:
[COLOR=#008080]evaluate[/COLOR]
[COLOR=#0000ff]crossjoin[/COLOR](
    [COLOR=#000000]'FactData'[/COLOR]
    ,[COLOR=#0000ff]addcolumns[/COLOR](
        [COLOR=#0000ff]summarize[/COLOR](
            [COLOR=#000000]'FactData'[/COLOR]
            ,[COLOR=#000000]'FactData'[Product][/COLOR]
        )
        ,[COLOR=#ff0000]"MinId"[/COLOR]
        ,[COLOR=#0000ff]calculate[/COLOR](
            [COLOR=#0000ff]min[/COLOR]([COLOR=#000000]'FactData'[Id][/COLOR])
        )
    )
)

but just get the error:

Code:
Function CROSSJOIN does not allow two columns with the same name 'FactData'[Product].

I'm probably going about this the wrong way, but any help would be much appreciated - can upload a workbook if required (but i'll have to make it first)
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The answer may depend a bit on the size of your data (cuz we may care more if you have a ton), but ignoring (probably) the best answer (just filter your data in SQL or add another column from SQL with useful stuff), I would probably do something like...

A calc column:
IsMin = FactData[Id] = CALCULATE(MIN(FactData[Id]), ALL(FactData), FactData[Product] = EARLIER(FactData[Product))

It would be TRUE, for any row where it's the smallest ID within that Product. Nice to use a Boolean cuz they are small n such :)

Then, you can easily filter your measures with that...
TotalMeasure := CALCULATE(SUM(Measure), FactData[IsMin] = TRUE)
 
Upvote 0
sounds good, I'll give it a go.

I was driving home last night and thought about doing it in T-SQL, as there is quite a lot of data and that is going to be very fast.

However, I thought I'd try and expand my brain a bit by doing in DAX too... looks like i massively had the wrong end of the stick!! ;)

I still don't 'get it' 100% but am getting there, cheers!!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top