Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
PBI_Rocks
Frequent Visitor

SQL Server Datatype -> Power Query Fixed decimal number ($)

Hi,

Is there a SQL datatype that is recognised by PQ as Fixed decimal number.

Decimal(19,4) , MONEY and SMALLMONEY are all recognised as a Decimal number.

 

The column can be converted using Table.TransformColumnTypes(TestTable,{{"DecimalDef", Currency.Type}}) but this appears to 'break' folding.

 

Other than placing the conversion transform as the last step, Have I missed something obvious?

 

Thanks.

Regards,

B

 

 

 

 

1 ACCEPTED SOLUTION

Hi  @BA_Pete ,

It appears that the Value.NativeQuery does not work as I expected.

 

Created and populated a small test table.

 

CREATE TABLE [ref].[TestTable](

       [MoneyDEF] [MONEY] NOT NULL,

       [SmallMoneyDef] [SMALLMONEY] NOT NULL,

       [DecimalDef] [DECIMAL](19, 4) NOT NULL,

       [NumericDef] [NUMERIC](19, 4) NOT NULL,

       [FloatDef] [FLOAT] NOT NULL

) ON [PRIMARY]

 

 

PBI_Rocks_0-1661855149454.png

 

 

The following M gives the desired results in that the MONEY/SMALLMONEY columns are imported FIXED DECIMAL the others as FLOAT

 

let

    Source = Sql.Database("myServer", "myDatabase"),

    ref_TestTable = Source{[Schema="ref",Item="TestTable"]}[Data]

in

    ref_TestTable

 

I have been using Value.NativeQuery, which for some reason - as yet to be determined - does not import the MONEY/SMALLMONEY as FIXED DECIMAL all columns are FLOAT

 

let

  Source = Sql.Databases(Server),

  db = Source{[Name = Database]}[Data],

  TestTable = Value.NativeQuery(

    db,

    "SELECT

            [MoneyDEF],

            [SmallMoneyDef],

            [DecimalDef],

            [NumericDef]

     FROM [ref].[TestTable]",

    null,

    [EnableFolding = true]

  )

in

    TestTable

 

The first workaround is not to use Value.NativeQuery but this outcome is a little surprising to me.

 

Will try and look more into Value.NativeQuery.

 

Regards,

B

 

 

View solution in original post

8 REPLIES 8
BA_Pete
Super User
Super User

Hi @PBI_Rocks ,

 

The fact that converting to Currency.Type in PQ breaks folding tends to suggest that there isn't a direct equivalent that can pass from SQL to PQ.

Why do you want to do this in PQ anyway? I, personally, always see Currency/Fixed Decimal as formatting rather than data type changes and thus always make these changes in the front end.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hi Pete,
I agree - for the reason you gave - that it is possibly the case that there isn't a direct equivalent that can pass from SQL to PQ as currency datatype.

The datatype conversion needs to be done, because the rounding issues of Fixed Decimal are not acceptable to our users, so the Currency datatype is implemented.

 

Regards,

B

Hi @PBI_Rocks ,

 

If it's rounding of the values that's the problem, then you can use the Number.Round~ family of functions in Power Query. There's a few different types you can use, and each have different arguments you can use to get exactly the behaviour you need:

https://docs.microsoft.com/en-us/powerquery-m/number-round 

 

I believe all of these functions will fold to SQL Server.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hi BA_Pete,

I think that this article explains the requirement for the Fixed Decimal rather than Floating Point better than I can.

 

Choosing Numeric Data Types in DAX - SQLBI

 

Regards,

B

Hi @PBI_Rocks ,

 

Thanks for sharing the article, this makes sense.

Based on my own tests, both CASTing and CONVERTing numerical values to the SQL MONEY data type on SQL Server imports into Power Query correctly as the PQ Fixed Decimal data type:

BA_Pete_0-1661846515721.png

 

As such, I think there's two possible issues:

 

1) PQ is applying an automatic data type evaluation on import, for some reason (SQL Server is classified as a 'Structured Source', so this should never happen, but just in case). You would see this as an auto-generated 'Changed Types' step under your Source/Navigation steps and this should be deleted to revert back to the source data types.

 

2) Your source data isn't being held as MONEY type in the database. This, as above, can be corrected using either CAST or CONVERT on these fields in the DB. However, the question would remain as to whether you are actually getting the data accuracy/integrity that you desire, when the values aren't even being stored in the DB as this data type.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hi  @BA_Pete ,

It appears that the Value.NativeQuery does not work as I expected.

 

Created and populated a small test table.

 

CREATE TABLE [ref].[TestTable](

       [MoneyDEF] [MONEY] NOT NULL,

       [SmallMoneyDef] [SMALLMONEY] NOT NULL,

       [DecimalDef] [DECIMAL](19, 4) NOT NULL,

       [NumericDef] [NUMERIC](19, 4) NOT NULL,

       [FloatDef] [FLOAT] NOT NULL

) ON [PRIMARY]

 

 

PBI_Rocks_0-1661855149454.png

 

 

The following M gives the desired results in that the MONEY/SMALLMONEY columns are imported FIXED DECIMAL the others as FLOAT

 

let

    Source = Sql.Database("myServer", "myDatabase"),

    ref_TestTable = Source{[Schema="ref",Item="TestTable"]}[Data]

in

    ref_TestTable

 

I have been using Value.NativeQuery, which for some reason - as yet to be determined - does not import the MONEY/SMALLMONEY as FIXED DECIMAL all columns are FLOAT

 

let

  Source = Sql.Databases(Server),

  db = Source{[Name = Database]}[Data],

  TestTable = Value.NativeQuery(

    db,

    "SELECT

            [MoneyDEF],

            [SmallMoneyDef],

            [DecimalDef],

            [NumericDef]

     FROM [ref].[TestTable]",

    null,

    [EnableFolding = true]

  )

in

    TestTable

 

The first workaround is not to use Value.NativeQuery but this outcome is a little surprising to me.

 

Will try and look more into Value.NativeQuery.

 

Regards,

B

 

 

 

That's interesting, I wasn't aware of this bug feature.

Personally, I never use native queries, so have never come across this. If you're able to get views created on the DB using your native query script, that would always be my strong recommendation for many reasons, this one now added to the list!

I suppose you could add the CONVERT/CAST functions into your native query, would be interesting to see if that works.

If you get the time, it would be great if you could update this thread with your findings. I'm sure it would help future users.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hi @BA_Pete ,

The same result occurs with VALUE.NATIVEQUERY on views created on the test table.

 

If there are futher devlopments I will update this thread.

 

Thanks for your help.

 

Regards,

B

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.