Can someone please check my noob VAR formula? - powerbi-desktop

I've recently started working through the book "The Definitive Guide to DAX", but got stuck at one of the examples.
Data in visual:
Delivery Date 01/08/2007
SalesAmount $7,530.881
TotalCost $3,947.08
GrossMargin $3,583.801
Var GrosMarg% 5.31*
(Link to picture of visual with data: https://drive.google.com/file/d/1Xv2irn53zftdu9qn1njtcreyOK8nKQkO/view?usp=sharing)
As you can see, Var GrosMarg% is not correct. It's definitely not 5%, or 530% (which I get when I format it as a %).
But I really see nothing wrong with my VAR calculation:
Var GrosMarg% =
VAR TotalSales = sum(Sales[SalesAmount])
VAR TotalCosts = sum(Sales[TotalCost])
VAR GrossMargin = TotalSales - TotalCosts
Return
GrossMargin / TotalSales

Your DAX logic is correct. The only problem I can think is that you are creating a Column instead of a Measure. If that the case the results should be diferent.
As an example I created a column and a measure so you can see the diference.

Related

Update SQLite row with certain number

I have a problem that I can't solve with better-sqlite3 on node.js
I have a table looking somewhat like this:
image of table
How can I change each rows xp, that has a level of 3 to 100? So in this example it should change id's 1 and 4 xp value to 100.
Any help is appreciated!
This is a pretty basic SQL Query, you're just needing to do a WHERE on all levels that equal 3.
UPDATE tableName
SET level = 100
WHERE level = 3
Looking at the better-sqlite3 documentation, to do an UPDATE you simply need to call the function using run()
const stmt = db.prepare('UPDATE tableName SET level = ? WHERE level = ?');
const info = stmt.run(3, 100);

Error message "cannot find function getFullYear(...)" when entering date and trying to save the record

We are trying in a RESTLet to access the sublist "demandplandetail" from a NetSuite Item Demand Plan. Everything goes fine until a certain point. We are able to load it and process the demandplan for 2020. However, here it gets frustrating.
We know (can see from NetSuite) that there is data also for 2021. However, to access that from SuiteScript seems very difficult.
1st solution) The item demand plan has the field "year". OK, just set that to 2021, save and reload the record. Result: saving ignored, year still is 2020.
2nd solution) Set the year using a Date object as in:
var demandPlan = record.load(...)
var d = new Date();
demandPlan.setValue({
fieldId: 'year',
value: d
});
Gives the following:
:"TypeError: Cannot find function getFullYear in object NaN. (NLRecordScripting.scriptInit$lib#59)","stack":["setDatesForMonthAndYear(NLRecordScripting.scriptInit:108)","anonymous(N/serverRecordService)"
on saving the record. I also get the same using (various) strings adhering to acceptable date formats (as in '1/1/2021'). I have also tried the format package giving me a date string -> the same result.
Also read somewhere that you may need to set the start date (field 'startdate') in the record. Tried several variations but it stubbornly refuses :(.
Wonder if anyone has seen anything similar?
Best Regards,
Toni
Hi Please try the below code also check if you're passing date object to the field not the date string.
function formatDate() {
var dateROBD = format.parse({
value: new Date(),
type: format.Type.DATE
});
// this line optional if you want to try with or else ignore this
dateROBD = convertUTCDateToLocalDate(new Date(dateROBD));
return dateROBD;
}
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
OK, mystery solved. Turned out that this is not supported in SuiteScript 2.0 but you need to use 1.0.

calculate time difference in nodeJs

I am new to nodejs, and I am trying to get the difference between 2 times, but I get a different value.
here is my code :
var now = moment(Suivi.FI);
var then = moment(Suivi.DI);
var TA = moment(moment.duration(then.diff(now))).format("hh:mm");
as a result i get:
sql": "Insert into suivi(date, equipement, project, shift, description, Action, user, DA, DI, FI,TA) values('','','','','','','','','','','12:00')"
I tried to test with a simple date but I got the same value.
what am I missing? what should I do.?
you can use .format("hh:mm")
for simple time differnce:
var TA = now.diff(then);
this will give you more flexibility for formating (see docs):
var TA = moment.duration(now.diff(then));

Query by Date not working

I am trying to return search results for every document that was created on a day. Below is the query I use.
var query = Document.find({}).populate('contacts');
var gte = moment(req.query.date, 'DD-MM-YYYY').startOf('Day');
var lte = moment(req.query.date, 'DD-MM-YYYY').endOf('Day');
query.where('dates.createdAt').gte(gte).lt(lte);
This query works for some days but not all. I can't seem to understand the behaviour. Please help out.
The format of the date in the query string is DD/MM/YYYY.
Works for : 2016-04-16T00:02:30.065Z
Does not work for: 2016-04-15T19:02:59.758Z
It's wrong because you don't initialize as .utc(), and MongoDB dates are stored in UTC:
var gte = moment.utc(req.query.date, 'DD-MM-YYYY');
var lte = moment.utc(req.query.date, 'DD-MM-YYYY').endOf('Day');
And there is no need for the startOf() either.
If you don't construct like that, then the resulting Date object is skewed by the difference in the local timezone. Hence why you don't see the selection working where the hours would cross over dates.
Also if dates are coming in as 01/01/2016 then the format string would be 'DD/MM/YYYY', but one or the other is likely a typo in your question.

Displaying the total sum of values from strings

My project is a simple shopping game where the user types in the quantity amount, and the value of the individual prices appears and then a total sum can appear below.
I have managed to create the part of displaying the individual product price but i am confused on how to add the total sum and display correctly at the instance that i defined.
Some info
Actionscript will check for keypress event
sample of code snippet:
if(e.keyCode == 49){ //1
trace("Key Code Pressed: " + e.keyCode);
amount1.text = "1.00"
}
...
var total:Number = amount1+ amount2+amount3+amount4+amount5;
output1.text = String(total);
From the code above, when the user types 1, the price will change to "1.00" on the price instance field (dynamic text type).
Picture below:
A sample of my game running:
Total price should be $13.00 dollars..
Is there any way to make this happen? I believe is it something to do with parseint.
You should be able to string together multiple parseint statements like this:
var total:number = parseint(amount1.text) + parseint(amount2.text) + parseint(amount3.text) + parseint(amount4.text) + parseint(amount5.text);
output1.text = total;
If you go this route, you will need to handle stituations that involve NaN
Here is the documentation on parseint if you haven't already, you should look at it. http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000590.html

Resources