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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
EmersonSavage
Frequent Visitor

"Fatal error" when using MySQL variable in query

I am trying to create a visualization which shows the cumulative contributions to a portfolio over time and its market value at given dates.

 

I'm connecting to a MySQL database.  I initiate the connection normally by clicking New Source, entering the database credentials and my query, which includes a user-defined variable, @csum.  It appears that PBI does not recognize the user defined variable as it is "outside the session."  When I try to run the below, I receive the error, "MySQL: Fatal error encountered during command execution."

 

 

set @csum := 0;
select t.*, p.client_id, p.code, p.report_name1, if(t.type = 'WITHDR' or t.type = 'DELIV', -t.net_amount, t.net_amount) as Settlement_Amount, (select (@csum := @csum + Settlement_amount)) as cumulative_sum

 

from transaction t join portfolio p on t.portfolio_id = p.id

 

where t.trade_date > '2013-12-31' and (t.type = 'CONTR' OR t.type = 'WITHDR' OR t.type = 'ADD' OR t.type = 'DELIV') and t.sub_code <> 'ME' and p.code = 'REDACTED'

 

ORDER BY t.transaction_number

 

Does anyone know of a workaround for this?

1 ACCEPTED SOLUTION
v-haibl-msft
Microsoft Employee
Microsoft Employee

@EmersonSavage

 

I’ve got response from the Product Team.

 

This is outside the control of Power BI. We rely on the MySQL ADO.NET driver to correctly execute this code. It appears that in this case, it does not work.

 

Best Regards,
Herbert

View solution in original post

6 REPLIES 6
Dan_PCE
Regular Visitor

Yes! Workaround is to change @csum to @'csum'

 

This fixed my issue immediately! 

 

Kudos to https://stackoverflow.com/a/45605184/10089530

Great, that's it!

v-haibl-msft
Microsoft Employee
Microsoft Employee

@EmersonSavage

 

I’ve got response from the Product Team.

 

This is outside the control of Power BI. We rely on the MySQL ADO.NET driver to correctly execute this code. It appears that in this case, it does not work.

 

Best Regards,
Herbert

There is a solution: check post below

v-sihou-msft
Microsoft Employee
Microsoft Employee

@EmersonSavage

 

I reproduced same issue as you described. We have reported it interanlly.

 

11.PNG

 

22.PNG

 

33.PNG

 

Regards,

Chihiro
Solution Sage
Solution Sage

I'd imagine you can write query without using MySQL variable, using correlated query for cumulative sum column.

 

See link for some sample (Tom H's post).

https://stackoverflow.com/questions/439138/running-total-by-grouped-records-in-table

 

Another option may be to do cumulative sum calculation in PowerBI and not at query stage.

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors