Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

random sampling

Spoiler
 

How to take number of rows as input and show random set of rows ?

 

8 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      how to take row count from users?

      • Anonymous's avatar
        Anonymous
        Not applicable

        Create one table generateseries(1,1000)

         

        then add it in slicer

         

        Allow user to select number of samples

         

         

        and undate it in above formula .

        in var section add selectedvalue(table[series]) instead of 10.

         

        Thanks & regards,
        Pravin Wattamwar
        www.linkedin.com/in/pravin-p-wattamwar

        If I resolve your problem Mark it as a solution and give kudos.

  • Anonymous's avatar
    Anonymous
    Not applicable
    Of course, you can do that. Don't pay attention to those who say it's not possible. I know It IS possible. I'll have a think about it later today since right now I don't have time to create the solution.

    Best
    D
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi there. I'll give you an idea about how to do this. This is probably the only way to do it with the current version of DAX and Power BI.

       

      First, let's say you have a table (call it 'Data') in the model with N rows which are numbered consecutively, say, from 0 to (N-1) (the column is called Index). Now, you'll construct a table ('Samples') with these properties:

      1. The first column will hold numbers from 1 to K (there'll be many rows for each value).
      2. The second column will hold the indexes of rows that will be selected for a particular k and SampleId.
      3. The third column will hold the SampleId.

      Here's the structure:

      k Index SampleId
      1 2 1
      1 4

      2

      1 9 3
      2 1 1
      2 6 1
      2 8 2
      2 3 2
      3 4 1
      3 2 1
      3 8 1

      ...

       

      Can you see what's going on here? The k column will be sitting on one slicer, the SampleId on another. When you select a value of k, the other slicer will adjust the SampleId from 1 to the number of samples for that particular k. Now you just have to select a sample from the range slicer. This is how you'll obtain a (pseudo-)random selection of rows.

       

      For each k you have to create samples where each sample will be a unique combination of the indexes of the rows to select. For k = 1, you only need N samples to cover all possibilities. For k = 2, you need N*(N-1)/2 combinations, for k = 3, you need C(N, k) combinations... but this number of samples will be going up quickly as k increases. So, when k is relatively big you don't need to cover all combinations. Just pick enough of them. Once you have the above table, you can write a measure [Select Row] that will return 1 if the row should be selected and 0 if not. Here it is:

      [Select Row] =
      var __k = SELECTEDVALUE( 'Samples'[K] )
      var __oneSampleSelected = HASONEVALUE( 'Samples'[SampleId] )
      var __currentRowid = SELECTEDVALUE( 'Data'[Index] )
      var __selectRow =
      	__currentRowid in VALUES( 'Samples'[Index] )
      return
      	if( __oneSampleSelected, 1 * __selectRow )

       

      The visual in which you've placed the rows of the Data table should have a filter on it so that a row will display when [Select Row] is 1.

       

      Best

      D

      • Anonymous's avatar
        Anonymous
        Not applicable

        Thank you vey miuch ! Will try this and update how it goes