Forum Discussion
custom function creates but expression error when add column/invoke custom function getting
- 7 years ago
Hello, Thank you for your offer to help. Chris Webb from Cross Join was able to help me. I'll include the solution below for others to benefit from.
The correct and successful function is as follows -
let Fiscalyr1 = (UpgradeRpDate,CurrentMaint,UpgradeCost) => if Date.IsInCurrentYear(Date.Year(UpgradeRpDate))=Date.Year(UpgradeRpDate) then
CurrentMaint+UpgradeCost
else CurrentMaint
in Fiscalyr1
I was using square brackets incorrectly in the original one. You use square brackets when you’re calling the function because you’re getting values from the various columns on the current row. Inside the function, however, you’re working with function parameters and not columns on a row in a table.
This is now a solved issue. thanks again for reaching out to help!
Hello Xiaoxin,
Thank you for your reply. I appreciate your willingness to try and understand to help me.
Let me start with what I am trying to accomplish first and then maybe you can tell me a better/easier way to handle it.
I successfully created an excel worksheet to take the provided current year maintenance cost of equipment along with the next year maintenance cost to produce the projected cost for the next five years. I used 4 of the 10 provided columns (2,3,9,10) below. Here is a simple example to follow:
Current Yr Next year --------------------Fiscal Yr Cost----------------------------
Type Maint cost Maint cost 2019 2020 2021 2022 2023 update date upgrade cost
software 1,000 1,100 1,000 1,100 1,760 1,331 1,464 7/1/2021 550
to calculate cost for each fiscal year the formulas are below
IF(YEAR(NOW())=[],[Current Year Maintenance]+[Upgrade/Replace Cost],[Current Year Maintenance])
IFERROR(next yr maint cost/ current yr maint cost,0)
2019 Fiscal Yr Cost à IF(YEAR(NOW())=YEAR(update date),current yr maint cost+upgrade cost,current yr maint cost)
2020 Fiscal Yr Cost -> IF(YEAR(NOW())+1=YEAR(update date),( current yr maint cost * IFERROR(next yr maint cost/ current yr maint cost,0)
)+ upgrade cost, current yr maint cost * IFERROR(next yr maint cost/ current yr maint cost,0))
2021 Fiscal Yr Cost -> IF(YEAR(NOW())+2=YEAR(update date),( current yr maint cost * IFERROR(next yr maint cost/ current yr maint cost,0)^2)+ upgrade cost, current yr maint cost * IFERROR(next yr maint cost/ current yr maint cost,0)^2)
…
I’m having a hard time creating this formula as a function that works in powerBI or the M language which ever this is considered. I am able to create the function successfully but when I go to add column>invoke custom function (which is available) it gives me the error.
thanks for your help!
HI mcraven1845,
According to your discretion, it seems like you need to calculate result based on current column value. It possible to get current column label as parameter to calculate in dax formula.
Can you lease share a pbix file with same data structure fake data so that I can test to coding formula on it?
Regards,
Xiaoxin Sheng
- mcraven18457 years agoFrequent Visitor
I would love to share it but I'm not sure exactly what the pbix file is or how to share it here?
- Anonymous7 years agoNot applicable
HI mcraven1845,
You can upload to onedrive or google drive and share link here. If your data are from external datasource, you can copy part of your records to excel sheet and pack it with your pbix file for share.
Regards,
Xiaoxin Sheng
- mcraven18457 years agoFrequent Visitor
Hello, Thank you for your offer to help. Chris Webb from Cross Join was able to help me. I'll include the solution below for others to benefit from.
The correct and successful function is as follows -
let Fiscalyr1 = (UpgradeRpDate,CurrentMaint,UpgradeCost) => if Date.IsInCurrentYear(Date.Year(UpgradeRpDate))=Date.Year(UpgradeRpDate) then
CurrentMaint+UpgradeCost
else CurrentMaint
in Fiscalyr1
I was using square brackets incorrectly in the original one. You use square brackets when you’re calling the function because you’re getting values from the various columns on the current row. Inside the function, however, you’re working with function parameters and not columns on a row in a table.
This is now a solved issue. thanks again for reaching out to help!