cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
stevedep
Memorable Member
Memorable Member

Sharing: How to create test data using DAX!

Hi,

In this article, I will show how to generate sample data and how to add this to your data model. 

The result is a sales table and related dimensions:

stevedep_1-1614058299300.png

stevedep_2-1614058326565.png

This is the code to generate the sales table:

 

 

 

Sales = 
VAR _tbl =
    SELECTCOLUMNS (
        CROSSJOIN ( ROW ( "test", DATE ( 2020, 10, 3 ) ), GENERATESERIES ( 1, 60, 1 ) ),
        "StartDate", [test],
        "Increment", [Value]
    )
VAR _dates =
    ADDCOLUMNS ( _tbl, "NewDate", [StartDate] + [Increment] )
RETURN
    GENERATE (
        SELECTCOLUMNS (
            { "Vendor 1", "Vendor 2", "Vendor 3", "Vendor 4", "Vendor 5" },
            "Vendor", [Value]
        ),
        SELECTCOLUMNS (
            ADDCOLUMNS (
                CROSSJOIN ( _dates, GENERATESERIES ( 1, 10, 1 ) ),
                "open_time",
                    [NewDate]
                        + NORM.INV ( RAND (), 0.5 + ( [Increment] / 100 ), 0.08 ),
                "PRODUCT",
                    CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING )
                        & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING )
                        & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ),
                "sales_amount", NORM.INV ( RAND (), [Value], [Value] / 10 )
            ),
            "sales_date_time", [open_time],
            "PRODUCT", [PRODUCT],
            "sales_amount", [sales_amount]
        )
    )

 

 

 

You can add the above code by navigating to the ribbon in Power BI, select 'Modelling' to then select 'New Table'. Paste the above code in the editor. 

stevedep_4-1614059464022.png

Explanation

In the next part, I will break down the working of the code. 

First, we generate a series of dates:

stevedep_3-1614058549862.png

We started with a default date and added an incremental column, that column sets the number of days that we will add to the default data. 

Next is adding vendors:

 

 

 

     SELECTCOLUMNS (
            { "Vendor 1", "Vendor 2", "Vendor 3", "Vendor 4", "Vendor 5" },
            "Vendor", [Value]

 

 

 

For each vendor, all the dates (generated above) are added and a timestamp is added:

 

 

 

  SELECTCOLUMNS (
            ADDCOLUMNS (
                CROSSJOIN ( _dates, GENERATESERIES ( 1, 10, 1 ) ),
                "open_time",
                    [NewDate]
                        + NORM.INV ( RAND (), 0.5 + ( [Increment] / 100 ), 0.08 ),

 

 

 

A product is added by randomly selected characters from the UNICHAR array:

 

 

 

 "PRODUCT",
                    CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING )
                        & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING )
                        & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ),

 

 

 

A sales amount is added:

 

 

 

      "sales_amount", NORM.INV ( RAND (), [Value], [Value] / 10 )

 

 

 

Finally, the required columns are added:

 

 

 

            "sales_date_time", [open_time],
            "PRODUCT", [PRODUCT],
            "sales_amount", [sales_amount]
        )

 

 

 

With the above steps, we have created the fact table. With that table in place we can add the dimension tables by simply adding a table with DAX like below:

 

 

 

dimProducts = VALUES(Sales[PRODUCT])

 

 

 

As a final step, we add the relationships in the data model. That is hopefully on familiar grounds. 

 

You can have fun with generating data and creating art from it as well 🙂 , see below:

stevedep_5-1614060078299.png

The above image is from a response that I wrote when helping out somebody with a DAX challenge. 

 

Hope that you find it useful. An example is attached.

 

Kind regards, 

Steve. 

 

 

0 REPLIES 0

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors