+ Reply to Thread
Results 1 to 9 of 9

I need to get a Random Sample from data within a pivot table

  1. #1
    Registered User
    Join Date
    03-22-2017
    Location
    Arizona
    MS-Off Ver
    Office 365 32-bit
    Posts
    14

    I need to get a Random Sample from data within a pivot table

    Good morning I am trying to get a weekly random sample from a list within a pivot table. My columns are Week, doc type, doc ID. I need a random sample every week of one doc ID of every doc type. I would like the cell value returned (doc ID) vs. a rand number. Since every week I'll take a 25% sample size of each doc type, it would be helpful if I could avoid duplicates. Can this be done? The volume per week and doc type vary so the amount of random samples needed per week will vary as well. Here is a mock up of the pivot table data. Thank you!
    Attached Files Attached Files
    Last edited by patchpollito1; 08-04-2020 at 03:56 PM. Reason: adding workbook mock up

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: I need to get a Random Sample from data within a pivot table

    May be add helper column(s) in the source data or in data model.

    Using RAND() function. Then add percentile column. Filter for 25% on Pivot Table.

    But it's bit hard to help you without sample workbook. See yellow banner at the top for details.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    03-22-2017
    Location
    Arizona
    MS-Off Ver
    Office 365 32-bit
    Posts
    14

    Re: I need to get a Random Sample from data within a pivot table

    Thanks CK76!

    I've added a sample workbook

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: I need to get a Random Sample from data within a pivot table

    I'm a bit confused, you have sample size with decimal value...

    I assume that you need 2 Unique IDs if sample size = 1.25, 1 if it's 0.25 etc. Correct?

    Also, method I indicated in my previous post can't be used here, since you are only interested in distinct (unique) list of ID and not whole list in raw data.

    Are you still using Excel 2010? Or are you using more recent version?

  5. #5
    Registered User
    Join Date
    03-22-2017
    Location
    Arizona
    MS-Off Ver
    Office 365 32-bit
    Posts
    14

    Re: I need to get a Random Sample from data within a pivot table

    You are correct I would want to pull 1 sample if it is .25 and 2 if it 1+ and so on. I am not sure where to find version info but I did find this: Microsoft Excel for office 365 MSO(16.0.12527.*****)32-bit. Does that help?

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: I need to get a Random Sample from data within a pivot table

    Yes, that helps. I'd recommend updating your profile to reflect your current version (Office 365, 32bit).

    With Office 365, you should have access to data model and DAX formula.

    As well as access to new array formulas; Unique(), Filter() etc.

    In your case... best bet is to create data model and few DAX measures to accomplish your need (using RAND(), SAMPLE() and some variables in DAX measure).

    I'm out of time for the day, but will see if I have time tomorrow to create sample set up from your data, if others haven't helped you by then.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: I need to get a Random Sample from data within a pivot table

    Oh, in previous post I mentioned SAMPLE(), but I forgot that in Excel data model, you can't create calculated table using DAX.

    It's not ideal, but instead, all sampling are done purely in PowerQuery (M code).

    This method skips Piovt Table and generates table with random sample.

    Complete M code for reference.
    Please Login or Register  to view this content.
    Most of the steps you can follow in Query Editor, using "Applied Steps" pane.

    See attached sample. Each time you refresh the query table, it will return with new random sample.

    Key Concepts:
    - Index column must be added before "Rand" column is added. This is to ensure evaluation of Number.Rand() in row context and not in table context.
    - myList column holds subset table of table filtered on Group By columns. It is then sorted by Rand column to extract first N. Where N is [SampleSize].
    Attached Files Attached Files
    Last edited by CK76; 08-05-2020 at 12:31 PM. Reason: Added Key concepts

  8. #8
    Registered User
    Join Date
    03-22-2017
    Location
    Arizona
    MS-Off Ver
    Office 365 32-bit
    Posts
    14

    Re: I need to get a Random Sample from data within a pivot table

    Thank you CK76!

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: I need to get a Random Sample from data within a pivot table

    You are welcome and thanks for the rep.
    If you are satisfied with the solution provided, please mark the thread as solved using thread tools found at top of your initial post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Which data set has the best overlap with a random sample
    By snomis in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-20-2017, 10:05 AM
  2. Averaging a random sample from a data set
    By batraneha in forum Excel General
    Replies: 7
    Last Post: 02-10-2016, 11:19 AM
  3. Count in Pivot Table by Month - with random dates in data source
    By lindsgray in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-08-2015, 04:49 PM
  4. Sample of latest 20 entries into pivot table
    By cloudwalking in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 07-11-2013, 03:25 PM
  5. Sample Questions on Pivot Table / Charts
    By shakra in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-24-2012, 04:03 AM
  6. Replies: 4
    Last Post: 12-15-2005, 01:50 PM
  7. URGENT- Random Sample per range of data
    By Another Jennifer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2005, 10:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1