clickhouse-odbc icon indicating copy to clipboard operation
clickhouse-odbc copied to clipboard

Power BI Direct Query CAST to DECIMAL in DIVIDE function causing precision errors

Open alimans3 opened this issue 5 months ago • 3 comments
trafficstars

Describe the bug

I am running Power BI with Clickhouse connector and have a simple DAX measure:

SUMX(TABLE1, DIVIDE(TABLE1.COL1, RELATED(TABLE2.COL1)))

TABLE1.COL1 and TABLE2.COL1 are decimal(38,15) types.

TABLE2.COL1 (denominator) is losing precision and causing the multiplication to be wrong (multiplication by an integer rounded value)

Steps to reproduce

  1. As mentioned above

Expected behaviour

Code example

Error log

Query log

Got from system.query_log:

SELECT OTBL.xxx, OTBL.xxx, OTBL.xxx, OTBL.xxx, OTBL.xxx, ITBL.xxx, multiIf( OTBL.T1COL1 IS NULL, NULL, multiIf( ( CAST(OTBL.T2COL1 , 'DOUBLE') IS NULL ) OR ( CAST(OTBL.T2COL1, 'DOUBLE') = _CAST(0., 'Nullable(Float64)') ), NULL, OTBL.T1COL1 / CAST( CAST(OTBL.T2COL2, 'DOUBLE'), 'DECIMAL' ) ) ) AS C1,

FROM .... (normal select with joins)

The problem comes from: CAST( CAST(OTBL.T2COL2, 'DOUBLE'), 'DECIMAL' )

CAST to DECIMAL added here is causing the column to be changed to an whole number (removing scale)

Configuration

Environment

  • Driver version: 1.3.3.20250317
  • OS: Windows 11, Clickhouse 25.4.2 in docker
  • ODBC Driver manager: latest

ClickHouse server

  • ClickHouse Server version: 25.4.2
  • ClickHouse Server non-default settings, if any:
  • CREATE TABLE statements for tables involved:
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

alimans3 avatar Jun 03 '25 22:06 alimans3

with the latest version 1.4.1.20250523 The cast changed to:

CAST( CAST(OTBL.T2COL2, 'Float64'), 'DECIMAL' )

However the issue persists

alimans3 avatar Jun 03 '25 22:06 alimans3

Here is a minimal example for debugging:

  1. Create and populate a new test table
create table test (a Decimal(9, 3), b Decimal(9, 3)) engine MergeTree order by a;
insert into test values (1, 1.3);
  1. Start MS Power BI and import the table

  2. Create a new measure for this table

Measure = SUMX(test, DIVIDE(test[a], test[b]))

Image

  1. Drop the new column on the canvas to see the result.

Image

Notice that it shows the incorrect result: 1.00, while we expect it to be around 0.77.

This is not the result of rounding done by UI because we can inspect and execute the query to see exactly the same result (1.00)

select sum(`C1`) as `C1`
from
(
    select `a`,
        `b`,
        case
            when `a` is null
            then null
            else (case
                when cast(`b` as Float64) is null or cast(`b` as Float64) = 0.00
                then null
                else `a` / cast(cast(`b` as Float64) as Decimal)
            end)
        end as `C1`
    from `adventureworks`.`test`
) as `ITBL`

slabko avatar Jun 06 '25 15:06 slabko

Also interesting how Power BI behaves when a and b are declared Float64 instead of Decimal(9,3) -- now I can see no type conversions in the query at all, which is probably expected.

slabko avatar Jun 07 '25 14:06 slabko

I believe we need to move it to PowerBI repository. It's likely to be a lack of mapping to Decimal type with precision. see https://github.com/exasol/powerbi-exasol/blob/e16793eb666361460ab83dfb46b5cadfd2002ab3/Exasol/Exasol.pq#L195

mshustov avatar Jul 23 '25 12:07 mshustov

https://github.com/exasol/powerbi-exasol/issues/49 created in exasol repo

alimans3 avatar Jul 28 '25 23:07 alimans3

@alimans3 please create in https://github.com/ClickHouse/power-bi-clickhouse

mshustov avatar Jul 30 '25 10:07 mshustov

@mshustov https://github.com/ClickHouse/power-bi-clickhouse/issues/48

alimans3 avatar Jul 30 '25 10:07 alimans3