Adding foreign key to kdb existing table - foreign-keys

I need to add a foreign key to a table that I have imported using a csv
table:("SSSSSSSSSFFFFSSSSSFSSSSSSSSSSSSSSS"; enlist ",") 0:
`:table.csv
I do not want to have to redefine the whole table. is there a way to do this?

q)p:([p:`p1`p2`p3`p4`p5`p6]name:`nut`bolt`screw`screw`cam`cog;color:`red`green`blue`red`blue`red;weight:12 17 17 14 12 19;city:`london`paris`rome`london`paris`london)
q)sp:([]s:`s1`s1`s1`s1`s4`s1`s2`s2`s3`s4`s4`s1;p:`p$`p1`p2`p3`p4`p5`p6`p1`p2`p2`p2`p4`p5;qty:300 200 400 200 100 100 300 400 200 200 300 400)
q)
q)update `p$p from `sp
`sp
q)meta sp
c | t f a
---| -----
s | s
p | s p
qty| j
Defining a foreign key is similar to enumerating/casting and therefore an overload of $ is used.
`sp means that the table is updated in place.

Related

Calculate monthly balance in PowerBI

Data-Table:
DB-Fiddle
CREATE TABLE vouchers (
id SERIAL PRIMARY KEY,
event_date DATE,
credits_collected INT,
credits_redeemed INT
);
INSERT INTO vouchers
(event_date, credits_collected, credits_redeemed
)
VALUES
('2020-01-08', '900', '700'),
('2020-02-15', '500', '300'),
('2020-02-20', '100', '250'),
('2020-03-19', '600', '850'),
('2020-04-03', '450', '130');
SQL-Query:
SELECT
t1.event_month AS event_month,
t1.credits_collected AS credits_collected,
t1.credits_redeemed AS credits_redeemed,
SUM(t1.credits_collected - t1.credits_redeemed) OVER (
ORDER BY t1.event_month ASC ROWS UNBOUNDED PRECEDING) AS balance
FROM
(SELECT
DATE_PART('month', v.event_date) AS event_month,
SUM(v.credits_collected) AS credits_collected,
SUM(v.credits_redeemed) AS credits_redeemed
FROM vouchers v
GROUP BY 1
ORDER BY 1) t1
GROUP BY 1,2,3
ORDER BY 1;
Result:
event_month | credits_collected | credits_redeemed | balance
-------------|---------------------|--------------------|------------
1 | 900 | 700 | 200
2 | 600 | 550 | 250
3 | 600 | 850 | 0
4 | 450 | 130 | 320
I am loading the above data-table into PowerBI.
Now, I want to create a report that looks like the results I am getting with the SQL-Query above.
I am able to put credits_collected and credits_redeemed to the report.
However, I have no clue what DAX formula I need to calculated the balance for the end of each month.
Do you have any idea how I can solve this issue?
I could solve the issue with two steps:
Step 1: Implementing an additional column with this DAX formula:
column_balance_calculated_daily = CALCULATE(SUM(Tabelle1[credits_collected])-SUM(Tabelle1[credits_redeemed]),ALL('Tabelle1'),'Tabelle1'[event_date]<=EARLIER('Tabelle1'[event_date] ))
Step 2: Use the column in the following DAX formula:
balance = CALCULATE(MAX(Tabelle1[column_balance_calculated_daily]),ENDOFMONTH(Tabelle1[event_date]))

calculated value beetween two tables

i had two tables, one of sales and the other had a value that represents the area of a store.
the data of the sales table had the value of the store, and the other table too, something like this
venta_dia
| id | u_venta | store| .......
1 100 1
2 122 2
m2
| store | m2 |
1 1000
2 30
and i need to calculate the value of the total of sales/m2 value by store, and i try this
enter image description here
but it dont work, any solution?

Dynamic Validation List - Multiple Criteria

I want to create a data validation list that changes in size depending on 2 criteria. The sheet looks like this:
A | B | C
1 | 1 | 100
2 | 0 | 200
3 | 1 | 300
4 | 1 | 400
5 | 0 | 500
6 | 1
7 | 1
Column A is the reference number of a loan
Column B is an activation cell (1 = loan is activated, 0 = loan is not activated)
Column C is the loan amount
I want to create a validation list that shows the reference number of the loans that are activated (i.e. Column B = 1) AND that have a value in Column C (i.e. Column C is not blank).
I managed to create the validation list that adjusts itself for the blank cells, but I don't know how to account for Column B as a second criteria.
A VBA code to do this would also be helpful!
A simple formula in Column D copied to all cells below with show you a validated column with TRUE beside the valid rows
=IF(B2,IF(C2,TRUE,""),"")
looks like

How can I update the dynamic cache manually?

Scenario
Source -> Lkp(dim) -> Expr -> 2Targets (Dim Table and Fact table)
Here target dim table is cached and used as dynamic lookup.
Whenever a row comes from source, it is looked up in the cache.
If lkp is successful then ID is retrieved and inserted into Fact along with other columns.
If lkp fails then new ID is generated in the Expr by a Sub Procedure call to database and is inserted into both Dim and Fact table along with other columns.
Issue
The ID since it is not a lookup column, it needs a value to be assigned in dynamic cache.
I can use the associated expression to set either a static value or a sequence.
But I need this ID to be generated by the DB which will not happen unless you pass the lkp and get into Expr
If I use auto sequence from informatica, then the dim table and cache will be out of sync - one getting sequence from informatica and the other from DB.
I cannot use informatica seq because this mapping will have multiple instances running concurrently.
Illustration
Dim table
ID col1 col2
401 a 1
402 b 3
403 h 8
Lookup
ID col1 col2
401 a 1
402 b 3
403 h 8
Fact Table
fcol1 fcol2 fcol3 ID(foreign key to DIM)
aa 11 1 401
bb 33 4 401
dk 44 2 403
Source
col1 col2
h 8
v 6
v 6
The first record from source will go smoothly. It will insert a record into fact with ID 403 from lkp
Fact Table after first record
fcol1 fcol2 fcol3 ID(foreign key to DIM)
aa 11 1 401
bb 33 4 401
dk 44 2 403
eo 23 5 403
The second record from source doesn't find a match in the lkp and hence updates the cache instantly.
Then Expr will execute this logic. If(lkp failed) then call sp(seq_gen) else use the id fetched from lkp
The new ID will be inserted to both DIM and FACT
Problem is this newly generated ID is not available to the lookup cache. Instead it uses the value from associated port.
Fact Table after second record
fcol1 fcol2 fcol3 ID(foreign key to DIM)
aa 11 1 401
bb 33 4 401
dk 44 2 403
eo 23 5 403
fa 32 3 404
Dim table after second record
ID col1 col2
401 a 1
402 b 3
403 h 8
404 v 6
Lookup
ID col1 col2
401 a 1
402 b 3
403 h 8
<ap> v 6
where is associated port expression. We can write any expression here. How do I bring 404 here. I cannot write Expr expression here as I cannot check if the lookup failed or succeeded.
Fact Table after third record
fcol1 fcol2 fcol3 ID(foreign key to DIM)
aa 11 1 401
bb 33 4 401
dk 44 2 403
eo 23 5 403
fa 32 3 404
ep 53 6 <ap> (I expect 404)

Stata table: how to compute difference column without adding a new variable?

In a panel data set, I'm using
table Region TIME if TIME==2014 | TIME==2020 | TIME==2030 | TIME==2040, contents(sum BF ) row
to create the following table:
------------------------------------------
| TIME
Region | 2014 2020 2030 2040
----------+-------------------------------
701 | 26751 27941 29944 31477
702 | 10456 11354 12723 13788
704 | 41550 44481 49340 53273
706 | 44976 47535 51940 55573
709 | 43258 44398 46612 48191
711 | 6580 7011 7539 7856
713 | 9036 10139 11776 13194
714 | 3091 3284 3563 3750
716 | 9144 9730 10724 11543
719 | 5719 6292 7258 8036
720 | 11509 12161 13188 13919
722 | 21403 22344 23839 25006
723 | 4927 5094 5345 5447
728 | 2460 2576 2761 2906
|
Total | 240860 254340 276552 293959
------------------------------------------
I'd like to add a fifth column, which displays the difference between the year 2014 and 2040 in %.
Question: is this possible WITHOUT adding a new variable to the dataset? For instance by letting the fifth column being derived from a formula?
If not, how do I easily compute a new variable, taking account of the long format of the panel data set?
This isn't possible within table.
Your variable could be something like
egen total2014 = total(BF / (TIME == 2014)), by(Region)
egen total2040 = total(BF / (TIME == 2040)), by(Region)
gen pcdiff = 100 * (total2040 - total2014)/total2014
after which you can tabulate its (mean) value for each region. See Section 10 in http://www.stata-journal.com/sjpdf.html?articlenum=dm0055 for the first trick here.
You may need to go outside table for the tabulation, but if all else fails, collapse to a new dataset of totals and means.