Pivot counting distinct values (not unique) in single row (not columns)

Quari

New Member
Joined
Nov 28, 2019
Messages
5
Office Version
  1. 2013
Dear Forum members,

I hope I will provide you clearly what I need.
Excel 2013. Win 7
Formula: I have no idea what formula to use. In internet I found multiple examples how to do same but for columns.
I have following data:

Row numberTicketMonthBrand 1Brand 2Brand 3Brand 4Brand 5
1RITM13132012019-8FordFordKIAKIAToyota
2RITM13129252019-8KIAToyota
3RITM13107542019-9FordKIAKIA
4RITM13107592019-10ToyotaToyota

I need a pivot and chart at the end which will show me how many disctinct Brands appeared in given month while"ticket" is a value counted.
E.g. There were following number of Tickets with Brand:
2019-8 1x Ford (disctinct number of Fords for Month 2019-8 is 1 despite of the fact "Ford" is listed x2 in first row) and 2x KIA (row 1st [second occurance ignored] and 2nd) and 2x Toyota (row 1st and in row 2nd)
2019-9 1x Ford (row 3rd) and 1xKIA (row 3rd [second occurance ignored])
2019-10 1x Toyota (row 4th, [second occurance ignored])


I do not care how many times 'Ford' is listed in single row. It is important for me to count only first occurance and list it later in pivot table

Result table would looks like:
MonthFordKIAToyota
2019-8222
2019-9110
2019-10001

Thank you,
Quari
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi, an easy way for that is Power Query, first you need convert you data in a Table (Ctrl + t)
1. Step
creating_table_2.JPG


2. Go to Data menu and select From Table, this action will open Power Query
Goto_Data_2.JPG


3. Delete Changed Type step
Delete_changes.JPG

...
 
Upvote 0
Hi, an easy way for that is Power Query, first you need convert you data in a Table (Ctrl + t)
1. Step
View attachment 940

2. Go to Data menu and select From Table, this action will open Power Query
View attachment 941

3. Delete Changed Type step
View attachment 942

...

4. Select two first columns, go to Transform menu and select Unpivot Other Columns
Unpivot_other.jpg


5. Delete column named Attribute
Delete_column.JPG


6. Close & Load To / Close & Load, this action put data in Excel, but in this shape
Final.JPG


Please see this attachment It is only an idea

Hernan Torres
Mikel ERP
 
Upvote 0
Try this UDF

VBA Code:
Function CountDistinct(Period As Range, Brand As Range, Data As Range)
  Dim a As Variant, i As Long, j As Long, m As Long
  a = Data.Value
  For i = 1 To UBound(a, 1)
    If a(i, 1) = Period.Value Then
      For j = 2 To UBound(a, 2)
        If a(i, j) = Brand Then
          m = m + 1
          Exit For
        End If
      Next
    End If
  Next
  CountDistinct = m
End Function

----------------------------------------
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use TextOnly just like it was a built-in Excel function. For example,

Book1
ABCDEFG
1TicketMonthBrand 1Brand 2Brand 3Brand 4Brand 5
2RITM13132012019-8FordFordKIAKIAToyota
3RITM13129252019-8KIAToyotaToyota
4RITM13107542019-9FordKIAKIA
5RITM13107592019-10ToyotaToyota
6
7
8
9MonthFordKIAToyota
102019-8122
112019-9110
122019-10001
Hoja2
Cell Formulas
RangeFormula
B10:D12B10=CountDistinct($A10,B$9,$B$2:$G$5)
 
Upvote 0
@hernantorres23 @DanteAmor Thank you for your ideas - I will review them and check if I can reaplly.
I see I did a small mistake in Result table - it should looks like this:

Result table would looks like:
MonthFordKIAToyota
2019-81 (there was 2 initially)22
2019-9110
2019-10001

Is there are way to edit own post? I checked FAQ and guidelines but did not found that information.
 
Upvote 0
@DanteAmor I tested it and it works! Thank you for help and additional information (yes I am new with UDF and VBA) and I like this as I do not need to change anything in source data or transform it.

Now I have trickier question since counting is working like dream. I would like to select only particular records depends on value in another column e.g. only rows with Status "Closed" or "Closed Skipped" should be included while those with Status "Open" should be not taken into consideration. Pivot table can filter data but here?
Is it possible to change a formula you provided or UDF should be modified? I have some knowledge about scripts/SQL but I do not fully understand your UDF to be able to modify it by myself. Surely if I would study it longer + what particular syntax means.

Entry Data:
Row number​
Ticket​
Status​
Month​
Brand 1​
Brand 2​
Brand 3​
Brand 4​
Brand 5​
1​
RITM1313201​
Closed​
sie-19​
Ford​
Ford​
KIA​
KIA​
Toyota​
2​
RITM1312925​
Open​
sie-19​
KIA​
Toyota​
3​
RITM1310754​
Closed Skipped​
wrz-19​
Ford​
KIA​
KIA​
4​
RITM1310759​
Closed​
paź-19​
Toyota​
Toyota​

Result:
MonthFordKIAToyota
2019-81 1 (second KIA from Row #2 was excluded as Status is "Open"1 (2nd Toyota excluded)
2019-9110
2019-10001
 
Upvote 0
@hernantorres23 @DanteAmor Thank you for your ideas - I will review them and check if I can reaplly.
I see I did a small mistake in Result table - it should looks like this:

Result table would looks like:
MonthFordKIAToyota
2019-81 (there was 2 initially)22
2019-9110
2019-10001
Is there are way to edit own post? I checked FAQ and guidelines but did not found that information.

I'm so sorry, but these was only an idea, but if possible do it, you only need add two steps more, these are:
= Table.AddColumn(#"Removed Columns", "idx", each [Ticket]&[Month]&[Value])
= Table.AddColumn(#"Removed Columns", "idx", each [Ticket]&[Month]&[Value])

Then, your Power Query window Applied Steps (Advanced Editor) looks like:
final_M_language.JPG


And in Excel your Pivot Table looks:
Book1
ABCDEFGHIJKL
1TicketMonthValue
2Your data example RITM13132012019-8Ford
3TicketMonthBrand_1Brand_2Brand_3Brand_4Brand_5RITM13132012019-8KIA
4RITM13132012019-8FordFordKIAKIAToyotaRITM13132012019-8Toyota
5RITM13129252019-8KIAToyotaRITM13129252019-8KIA
6RITM13107542019-9FordKIAKIARITM13129252019-8Toyota
7RITM13107592019-10ToyotaToyotaRITM13107542019-9Ford
8RITM13107542019-9KIA
9 Your data example resultRITM13107592019-10Toyota
10MonthFordKIAToyota
112019-8222
122019-9110
132019-10001
14
15Pivot Table created by htorres
16Ticket__
17Row LabelsFordKIAToyotaGrand Total
182019-81225
192019-9112
202019-1011
21Grand Total2338
22
pivot-counting-disctinct-values


This is the attachment updated:::Pivot counting with Power Query
 
Upvote 0
Hi @Quari, try the following.

I would like to select only particular records depends on value in another column e.g. only rows with Status "Closed" or "Closed Skipped" should be included while those with Status "Open" should be not taken into consideration.

I adjusted the code to consider the status.
Now the data area should start in the status column, check the example below:

Book1
ABCDEFGHI
1NumberTicketStatusMonthBrand 1Brand 2Brand 3Brand 4Brand 5
21RITM1313201Closed2019-8FordFordKIAKIAToyota
32RITM1312925Open2019-8KIAToyotaToyota
43RITM1310754Closed Skipped2019-9FordKIAKIA
54RITM1310759Closed2019-10ToyotaToyota
6
7
8
9MonthFordKIAToyota
102019-8111
112019-9110
122019-10001
Bands
Cell Formulas
RangeFormula
B10:D12B10=CountDistinct($A10,B$9,$C$2:$I$5)


Try this:
VBA Code:
Function CountDistinct(Period As Range, Brand As Range, Data As Range)
  Dim a As Variant, i As Long, j As Long, m As Long
  a = Data.Value
  For i = 1 To UBound(a, 1)
    Select Case LCase(a(i, 1)) 'first column of Data
      Case LCase("Open")
      Case LCase("Closed"), LCase("Closed Skipped")
        If a(i, 2) = Period.Value Then  'second column of data
          For j = 2 To UBound(a, 2)
            If a(i, j) = Brand Then     'brands columns
              m = m + 1
              Exit For
            End If
          Next
        End If
    End Select
  Next
  CountDistinct = m
End Function
 
Upvote 0
@DanteAmor I tested it and it works! Thank you for help and additional information (yes I am new with UDF and VBA) and I like this as I do not need to change anything in source data or transform it.

Now I have trickier question since counting is working like dream. I would like to select only particular records depends on value in another column e.g. only rows with Status "Closed" or "Closed Skipped" should be included while those with Status "Open" should be not taken into consideration. Pivot table can filter data but here?
Is it possible to change a formula you provided or UDF should be modified? I have some knowledge about scripts/SQL but I do not fully understand your UDF to be able to modify it by myself. Surely if I would study it longer + what particular syntax means.

Entry Data:
Row number​
Ticket​
Status​
Month​
Brand 1​
Brand 2​
Brand 3​
Brand 4​
Brand 5​
1​
RITM1313201​
Closed​
sie-19​
Ford​
Ford​
KIA​
KIA​
Toyota​
2​
RITM1312925​
Open​
sie-19​
KIA​
Toyota​
3​
RITM1310754​
Closed Skipped​
wrz-19​
Ford​
KIA​
KIA​
4​
RITM1310759​
Closed​
paź-19​
Toyota​
Toyota​
Result:

MonthFordKIAToyota
2019-811 (second KIA from Row #2 was excluded as Status is "Open"1 (2nd Toyota excluded)
2019-9110
2019-10001

Of course you can do it, in addition you can take the advantages of M language, if you are thinking in Power BI
Let me show you the code for Advanced Editor in Power Query

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Ticket", "Status", "Month"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Idx", each [Ticket]&[Month]&[Value]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Status] <> "Open")),
    #"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Idx"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Removed Duplicates",{"Status"})
in
    #"Removed Columns1"

Here the result:
Book1
ABCDEFGHIJKLMNOP
1
2Your data example
3TicketStatusMonthBrand 1Brand 2Brand 3Brand 4Brand 5TicketMonthValueIdx
4RITM1313201Closedsie-19FordFordKIAKIAToyotaRITM1313201sie-19FordRITM1313201sie-19Ford
5RITM1312925Opensie-19KIAToyotaRITM1313201sie-19KIARITM1313201sie-19KIA
6RITM1310754Closed Skippedwrz-19FordKIAKIARITM1313201sie-19ToyotaRITM1313201sie-19Toyota
7RITM1310759Closedpaź-19ToyotaToyotaRITM1310754wrz-19FordRITM1310754wrz-19Ford
8RITM1310754wrz-19KIARITM1310754wrz-19KIA
9 Your data example resultRITM1310759paź-19ToyotaRITM1310759paź-19Toyota
10MonthFordKIAToyota
112019-8222
122019-9110
132019-10001
14
15Pivot Table created by htorres
16Count of Ticket_
17Row LabelsFordKIAToyotaGrand Total
18sie-191113
19wrz-19112
20paź-1911
21Grand Total2226
22
23
pivot-counting-disctinct-va(2)
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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