Forum Discussion

PavelAndreev's avatar
PavelAndreev
New Member
5 years ago
Solved

Adding new column with a grouped value.

Hi everyone,

I have the following sample file:

SegmentCountryProductDiscount Band
GovernmentGermanyCarreteraNone
GovernmentGermanyPaseoNone
GovernmentCanadaPaseoNone
GovernmentGermanyVeloNone
GovernmentFranceVTTNone
GovernmentFranceAmarillaNone
GovernmentMexicoCarreteraLow
GovernmentFranceCarreteraLow
GovernmentFrancePaseoLow

 

I want to do the following:

  • Create additional column which includes distinct q-ty of products for each country. The result should be like in the table below: For each row of the table: If Country="Germany", then 3; If Country="France", then 4; If Country="Canada", then 1; If Country="Mexico", then 1. 

Question: Is it possible to add such column to the existing table? How?

SegmentCountryProductDiscount BandQ-ty of products
GovernmentGermanyCarreteraNone3
GovernmentGermanyPaseoNone3
GovernmentCanadaPaseoNone1
GovernmentGermanyVeloNone3
GovernmentFranceVTTNone4
GovernmentFranceAmarillaNone4
GovernmentMexicoCarreteraLow1
GovernmentFranceCarreteraLow4
GovernmentFrancePaseoLow4

 

  • Create a measure which returns the amount of countries which have more than 1 product. The result should be 2 (Canada and Mexico)
    Question: Is it possible to create a measure without creating new table.

I did it in the following way, but I want to improve it:

  • Create a new table (answer to the first question):

 

NewTable = 
		VAR t1 = 
			SUMMARIZECOLUMNS('Sample'[Country] , 'Sample'[Product])
		VAR t2 = 
			GROUPBY(t1,
			'Sample'[Country],
			"NUmofProducts", COUNTX(CURRENTGROUP(),1))
		RETURN
			t2

 

  • Create a new measure for the NewTable (answer to the second question):

 

NewMeasure = 
    CALCULATE(COUNTX(NewTable,NewTable[NUmofProducts]),NewTable[NUmofProducts]>1)

 

How can I receive the same results without creating a new table?
Thanks in advance!

  • PavelAndreev 

    what's the purpose of create the addtional column? you didn't use it when you create a new table

    you can try this measure

    Measure =
    VAR tbl=SUMMARIZE('Sample','Sample'[Country],"product",count('Sample'[Product]))
    return COUNTROWS(FILTER(tbl,[product]>1))
  • PavelAndreev You could create a new column in your existing table like this:

    Column = COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER('Table',[Country]=EARLIER('Table'[Country])),"Product",[Product])))

5 Replies

  • PavelAndreev 

    what's the purpose of create the addtional column? you didn't use it when you create a new table

    you can try this measure

    Measure =
    VAR tbl=SUMMARIZE('Sample','Sample'[Country],"product",count('Sample'[Product]))
    return COUNTROWS(FILTER(tbl,[product]>1))
  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    PavelAndreev You could create a new column in your existing table like this:

    Column = COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER('Table',[Country]=EARLIER('Table'[Country])),"Product",[Product])))