Using Power Query to Get Distinct Count

seaottr

Board Regular
Joined
Feb 10, 2010
Messages
60
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I'm struggling to figure this one out...I've searched online and have tried suggestions for other similar requests, but to no avail...

In my data set, I have Survey IDs (unique for each survey), but trying to add a column (Survey #) that indicates what survey # it is for that particular individual. I want to keep my data in the original format but just with the extra column added.

Here are two different views I have of the data, and I can use either depending on which is more efficient (and/or easier to produce the solution):

The "Survey #" column is the one I want to add.

Data Format 1:
Date​
Name​
Survey ID​
Survey #
1-10-2023​
John Smith​
123456​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-18-2023​
John Smith​
345678​
#2​
1-15-2023​
Peter Griffin​
456789​
#1​
1-16-2023​
Peter Griffin​
567890​
#2​
1-21-2023​
John Smith​
678901​
#3​


Data Format 2:
Date​
Name​
Survey ID​
Survey #
1-10-2023​
John Smith​
123456​
#1​
1-10-2023​
John Smith​
123456​
#1​
1-10-2023​
John Smith​
123456​
#1​
1-10-2023​
John Smith​
123456​
#1​
1-10-2023​
John Smith​
123456​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-16-2023​
Lisa Simpson​
234567​
#1​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-18-2023​
John Smith​
345678​
#2​
1-15-2023​
Peter Griffin​
456789​
#1​
1-15-2023​
Peter Griffin​
456789​
#1​
1-15-2023​
Peter Griffin​
456789​
#1​
1-15-2023​
Peter Griffin​
456789​
#1​
1-15-2023​
Peter Griffin​
456789​
#1​
1-15-2023​
Peter Griffin​
456789​
#1​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-16-2023​
Peter Griffin​
567890​
#2​
1-21-2023​
John Smith​
678901​
#3​
1-21-2023​
John Smith​
678901​
#3​
1-21-2023​
John Smith​
678901​
#3​
1-21-2023​
John Smith​
678901​
#3​

Any help would be greatly appreciated!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Using your first example here is the Mcode

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Name"}, {{"Data", each _, type table [Date=text, Name=text, Survey ID=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "Index",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Date", "Survey ID", "Index"}, {"Date", "Survey ID", "Index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Data"})
in
    #"Removed Columns"
NameDateSurvey IDIndex
John Smith1-10-20231234561
John Smith1-18-20233456782
John Smith1-21-20236789013
Lisa Simpson1-16-20232345671
Peter Griffin1-15-20234567891
Peter Griffin1-16-20235678902
 
Upvote 1
Solution
Thanks alansidman...However, I seem to be getting the same result as I did after trying other similar approaches. The Index column is still just listing results as 1, 2, 3, 4, 5, 6. Which data set from my original post were you using for your code? Also, I'm curious about the "[Date=text, Name=text, Survey ID=text]" portion. Why is the date field listed as "text"?
 
Upvote 0
Used the first example. As to the date, when I copied and pasted from your example, it would not let me make it date. Obviously, you can change. I did not retype your data. In the future, use XL2BB so that formatting is what you want. When you post as you did, all data copied comes over as text.

Here is a link to my file of your data and the step by step solution
 
Upvote 1
Thank you so much alansidman! Really appreciate the follow-up! After a couple of tweaks based on some additional fields I have in my actual data set and applying your code with the 1st data set format, I was able to get it to work!

Thank so much for the quick response(s)!
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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