4

I have a table Table1:

F1 | F2
-------
a  | 1
a  | 2
b  | 1
c  | 2

I would like to define a measure MyMeasure that would allow me to generate a table:

F2 | Count
----------
1  | 2
2  | 1

Where the values in Count would come from CUBEVALUE function like:

=CUBEVALUE("ThisWorkbookDataModel","[Measures].[MyMeasure]","[Table1].[F2].&[1]")
=CUBEVALUE("ThisWorkbookDataModel","[Measures].[MyMeasure]","[Table1].[F2].&[2]").

The logic of MyMeasure should be:

1) Find minimum value F2 for each group in F1 (a -> 1, b -> 1, c -> 2).

2) Distinct count items in F1 for a specified value from the previous point.

I was able to do it using a helper column in data model but I would like to achieve it with with a single measure.

The most recent attempt is:

=COUNTROWS(
    GENERATE(
        all(Table1[F2])
        ,SUMMARIZE(
            all(Table1),Table1[F1],"sth",min(Table1[F2])
        )
    )
)

As I understand I am messing up the context and as a result I am getting:

F2 | Count
----------
1  | 6
2  | 6

In T-SQL I can generate the output using a query:

select distinct
    t1.F2
    ,count(*) over (partition by t1.F2)
from (select distinct F2 from Table1) t1
    right join (select F1, min(F2) as MyMin from Table1 group by F1) t2
    on t1.F2 = t2.MyMin
zaptask
  • 687
  • 1
  • 8
  • 18

2 Answers2

1

I put my two cents in, you could create a calculated table which returns the distinct [F2] values and the count of their minimum occurrences in two columns. I am unsure you can get the minimum values count from only one measure.

CalculatedTable =
GROUPBY (
    GROUPBY ( Table, Table[F1], "Minimum", MINX ( CURRENTGROUP (), Table[F2] ) ),
    [Minimum],
    "MinCount", COUNTAX ( CURRENTGROUP (), [Minimum] )
)

Let me know if this helps.

alejandro zuleta
  • 13,962
  • 3
  • 28
  • 48
0

I may have misunderstood your problem, but I would do it with an array formula in a third column of Table1 which you could then easily sum by F2 as part of the data model:

{=IF([@F2]=MIN(IF([F1]=[@F1],[F2])),1,0)}
NBut
  • 1
  • 1