How to find the column based on the minimum value and the position - group-by

A new column should be added as a function of the condition.
Match Condition
eg : ID 1877
1.) In the id group, the minimum value of tho_cw_diff should be checked and Rail position of thor_pos/CW_pos should be same.
eg : ID 7931
2.) If the rail position is not the same for the minimum tho_cw_diff value, check for the other minimum value in group ID that the rail position thor_pos/CW_po is the same.
eg: ID 8880
3.) If all the rail position is different for the group ID, select the minimum tho_cw_diff value.
According to these three conditions, the new column must be updated.
Sharing screenshot data of the expected outputs below

You can try this one.
VAR ID = [ID]
VAR CV_POS = [CV_POS]
VAR tmpTbl =
CALCULATETABLE(
tbl
,ALL()
,tbl[ID] = ID
,tbl[THO_position] = CV_POS
,tbl[CV_POS] = CV_POS
)
VAR Result =
IF(
ISEMPTY(tmpTbl)
,MIN(tbl[THO_CV_DIFF]) -- may be you can write just [THO_CV_DIFF]. I didn't check
,MINX(tmpTbl,[THO_CV_DIFF])
)
RETURN
Result
I didn't check, so come with a feedback.
By the way, you can add you table by using of following symbols: "|" and "-"
myColumn1
myColumn2
A
1
B
2

Related

Dax measure to calculate minimum value with in a group of a complete table

The ask is to create a measure (not a calculated column using Earlier), to fetch minimum unit price of a product group by year and "UnitofMeasure" and show them as "UnitPriceMin" against complete list of data in separate column as shown below-
You can try with this below measure.
I have considered column "Year", "description" and "unitmeasure" for the grouping. You can add/remove columns as per your necessity.
Considered your table name - "product_details". change it as per your table name.
group_wise_min =
VAR current_row_year = MIN(product_details[year])
VAR current_row_product = MIN(product_details[description])
VAR current_row_unit_measure = MIN(product_details[unitmeasure])
RETURN
CALCULATE(
MIN(product_details[unitprice]),
FILTER(
ALL(product_details),
product_details[year] = current_row_year
&& product_details[description] = current_row_product
&& product_details[unitmeasure] = current_row_unit_measure
)
)
Output will be as below-

Filter portal for most recently created record by group

I have a portal on my "Clients" table. The related table contains the results of surveys that are updated over time. For each combination of client and category (a field in the related table), I only want the portal to display the most recently collected row.
Here is a link to a trivial example that illustrates the issue I'm trying to address. I have two tables in this example (Related on ClientID):
Clients
Table 1 Get Summary Method
The Table 1 Get Summary Method table looks like this:
Where:
MaxDate is a summary field = Maximum of Date
MaxDateGroup is a calculated field = GetSummary ( MaxDate ;
ClientIDCategory )
ShowInPortal = If ( Date = MaxDateGroup ; 1 ; 0 )
The table is sorted on ClientIDCategory
Issue 1 that I'm stumped on: .
ShowInPortal should equal 1 in row 3 (PKTable01 = 5), row 4 (PKTable01 = 6), and row 6 (PKTable01 = 4) in the table above. I'm not sure why FM is interpreting 1Red and 1Blue as the same category, or perhaps I'm just misunderstanding what the GetSummary function does.
The Clients table looks like this:
Where:
The portal records are sorted on ClientIDCategory
Issue 2 that I'm stumped on:
I only want rows with a ShowInPortal value equal to 1 should appear in the portal. I tried creating a portal filter with the following formula: Table 1 Get Summary Method::ShowInPortal = 1. However, using that filter removes all row from the portal.
Any help is greatly appreciated.
One solution is to use ExecuteSQL to grab the Max Date. This removes the need for Summary functions and sorts, and works as expected. Propose to return it as number to avoid any issues with date formats.
GetAsTimestamp (
ExecuteSQL (
"SELECT DISTINCT COALESCE(MaxDate,'')
FROM Survey
WHERE ClientIDCategory = ? "
; "" ; "";ClientIDCategory )
)
Also, you need to change the ShowInPortal field to an unstored calc field with:
If ( GetAsNumber(Date) = MaxDateGroupSQL ; 1 ; 0 )
Then filter the portal on this field.
I can send you the sample file if you want.

How to add blank lines to complete an Access report so that it matches a printed form?

I deal with a bunch of government forms, and I constantly find myself trying to get Access 2013 to output a report that matches a pre-printed form.
Most of the forms are now in .pdf form, so Access doesn't handle them well (to my knowledge) without additional software, which I cannot install on user production computers.
So I usually re-create the form as an Access report, but I have real difficulty when I have only enough records for a half a page.
How do I get a report to print the required records, and then fill the page with blank records so the "form" looks correct?
I'd be willing to try any possible solution - I've even gone so far as to create blank records to try to make this work. My goal, however, is to automate this process so any user can generate the report and it prints out correctly without a bunch of fiddling.
On this form, some or all of the lines might be used, and each person (I have ~550 people who each have an individual form) has a different number of lines, depending on the number of jumps they have completed.
I would have a dummy table with just a single numeric field called id. Populate this with a number of records greater than the biggest number of 'extra' records you're ever going to need to fill your form, using numbers from 1 upwards for id. Now say your original record source query for the report is:
select field1, field2, field3, field4
from myTable
order by field1
and you always want 15 rows to fill your form, then change the report's record source to:
select top 15 sort_seq, field1, field2, field3, field4
from (
select 0 as sort_seq, field1, field2, field3, field4
from myTable
union
select id, null, null, null, null
from dummyTable
) as X
order by sort_seq, field1
Ok, so you are recreating the complete PDF form as Access report. And the lower part is the details section of a continuous report.
The easiest way is indeed to create the correct number of empty records, but do this automatically (e.g. using DAO Recordset.AddNew).
I like to use a separate dummy table for this (with the same structure as the original table) to not pollute the original table with dummy records.
The form has 20 lines, so if your original table has 7 records for the current person, insert 13 records into the dummy table, using a large PK so they get sorted to the bottom.
Then use a query as record source for the report, joining the master table with a UNION of Jumps and Dummy table.
Alternatively there is the Report.Line() method: https://msdn.microsoft.com/en-us/library/office/ff198297.aspx
But this will be a lot more complicated to get it right.
I hope you have found the solution to this issue. I am posting this in the hope that if someone else finds this same question they will find this one as the best possible solution, as I have been struggling for about 2 days now.
I suppose you have at least two tables and a query where you mix information from both, and your report is made out from this query. I will call this your Query1, so the trick is that when you open the Report from your Form, you will use the label number of your Form to make the filter of the Query1 from where you will get the information for your report. Like this:
rectotal = DCount("*", "Quote-Report", "[Quote_No]=" & stQtNo)
This instruction will count how many records you have on your “Quote-Report” Query that are of the same value of the [Quote_No] field inside your Quote Form , as you are assigning the stQtNo variable to the Form with the name “Quote”; and put this value on your rectotal variable.
Now with rectotal you need to determine how many lines you want in your report. I used 28 so the following formula will give how many blank rows needed to fill the page.
If rectotal / 28 - Int(rectotal / 28) = 0 Then
x = 28
Else
x = 28 * (rectotal / 28 - Int(rectotal / 28))
End If
blanklines = 28 - x
tLines = rectotal + blanklines
Of course you can use 15, 20 or as many rows you want your report to print per page. The variable blanklines will give you this number.
Then I use the recordset procedure from Access to open the table where I have the information that will print in the detail section of the report and fill with as many records as I need using the blanklines variable and the Do Until Loop.
Set db = CurrentDb
Set rs = db.OpenRecordset("Item_QD", dbOpenDynaset, dbseechanges)
Item_QD is the table
where I want to insert new records to fill the report detail section.
x = 0
rs.MoveLast
Do Until x = blanklines
rs.AddNew
rs![Quote_No] = stQtNo
rs![CODE No] = "Z" & x I use the Z and the number so I can erase the records once I don’t need them anymore.
x = x + 1
rs.Update
Loop
Please note that you need to Dim db As Database and Dim rs As Recordset variables outside your private sub in module header. Also your Access needs to be running these two classes so you can use them.
Finally once you open your report you have to go to your detail section and do a requery command to refresh all the rows prior to print. Need to format your detail cells in order to print the grid line and the rest is history.
The beauty of this procedure is that you can add and handle as many rows as you want or need inside your report and make a professional look of your quotes, packing slips, purchase orders, etc.
Hope this solves many problems. For a fast reference on how to use and manage ACCESS record set visit: https://www.youtube.com/watch?v=WNm17l54z1c
Private Sub Report_Open(Cancel As Integer)
Dim blanklines, tLines As Integer
Dim rectotal, x As Long
Dim stQtNo As String
Set db = CurrentDb
Set rs = db.OpenRecordset("Item_QD", dbOpenDynaset, dbseechanges)
stQtNo = Forms![Quote].[Quote_No]
rectotal = DCount("*", "Quote-Report", "[Quote_No]=" & stQtNo)
If rectotal / 28 - Int(rectotal / 28) = 0 Then
x = 28
Else
x = 28 * (rectotal / 28 - Int(rectotal / 28))
End If
blanklines = 28 - x
tLines = rectotal + blanklines
x = 0
rs.MoveLast
Do Until x = blanklines
rs.AddNew
rs![Quote_No] = stQtNo
rs![CODE No] = "Z" & x
x = x + 1
rs.Update
Loop
End Sub
Another solution. Code behind report. I use it for a simple report without grouping and row height doesn't change.
Option Compare Database
Option Explicit
Public totalCount As Integer
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
printBlankRecords getnumberofBlanks
End Sub
Public Sub printBlankRecords(numberBlanks As Integer)
Dim recordCount As Integer
recordCount = DCount("*", Me.RecordSource)
totalCount = totalCount + 1
If totalCount = recordCount Then
Me.NextRecord = False
'once you get to the last record, stay on last record
ElseIf totalCount > recordCount And totalCount < (recordCount + numberBlanks) Then
Me.NextRecord = False
'make the font and backcolor the same appearing to be empty record for each field
Me.RateID.ForeColor = Me.RateID.BackColor
End If
End Sub
Public Function getnumberofBlanks() As Integer
Dim recordCount As Integer
Dim N As Integer, X As Integer
X = 20
recordCount = DCount("*", Me.RecordSource)
If recordCount <= X Then
getnumberofBlanks = X - recordCount
Else
N = 1
Do While N * 21 + X <= recordCount
N = N + 1
Loop
getnumberofBlanks = (N * 21 + X) - recordCount
End If
End Function

One to Many equivalent in Cassandra and data model optimization

I am modeling my database in Cassandra, coming from RDBMS. I want to know how can I create a one-to-many relationship which is embedded in the same Column Name and model my table to fit the following query needs.
For example:
Boxes:{
23442:{
belongs_to_user: user1,
box_title: 'the box title',
items:{
1: {
name: 'itemname1',
size: 44
},
2: {
name: 'itemname2',
size: 24
}
}
},
{ ... }
}
I read that its preferable to use composite columns instead of super columns, so I need an example of the best way to implement this. My queries are like:
Get items for box by Id
get top 20 boxes with their items (for displaying a range of boxes with their items on the page)
update items size by item id (increment size by a number)
get all boxes by userid (all boxes that belongs to a specific user)
I am expecting lots of writes to change the size of each item in the box. I want to know the best way to implement it without the need to use super columns. Furthermore, I don't mind getting a solution that takes Cassandra 1.2 new features into account, because I will use that in production.
Thanks
This particular model is somewhat challenging, for a number of reasons.
For example, with the box ID as a row key, querying for a range of boxes will require a range query in Cassandra (as opposed to a column slice), which means the use of an ordered partitioner. An ordered partitioner is almost always a Bad Idea.
Another challenge comes from the need to increment the item size, as this calls for the use of a counter column family. Counter column families store counter values only.
Setting aside the need for a range of box IDs for a moment, you could model this using multiple tables in CQL3 as follows:
CREATE TABLE boxes (
id int PRIMARY KEY,
belongs_to_user text,
box_title text,
);
CREATE INDEX useridx on boxes (belongs_to_user);
CREATE TABLE box_items (
id int,
item int,
size counter,
PRIMARY KEY(id, item)
);
CREATE TABLE box_item_names (
id int PRIMARY KEY,
item int,
name text
);
BEGIN BATCH
INSERT INTO boxes (id, belongs_to_user, box_title) VALUES (23442, 'user1', 'the box title');
INSERT INTO box_items (id, item, name) VALUES (23442, 1, 'itemname1');
INSERT INTO box_items (id, item, name) VALUES (23442, 1, 'itemname2');
UPDATE box_items SET size = size + 44 WHERE id = 23442 AND item = 1;
UPDATE box_items SET size = size + 24 WHERE id = 23442 AND item = 2;
APPLY BATCH
-- Get items for box by ID
SELECT size FROM box_items WHERE id = 23442 AND item = 1;
-- Boxes by user ID
SELECT * FROM boxes WHERE belongs_to_user = 'user1';
It's important to note that the BATCH mutation above is both atomic, and isolated.
Technically speaking, you could also denormalize all of this into a single table. For example:
CREATE TABLE boxes (
id int,
belongs_to_user text,
box_title text,
item int,
name text,
size counter,
PRIMARY KEY(id, item, belongs_to_user, box_title, name)
);
UPDATE boxes set size = item_size + 44 WHERE id = 23442 AND belongs_to_user = 'user1'
AND box_title = 'the box title' AND name = 'itemname1' AND item = 1;
SELECT item, name, size FROM boxes WHERE id = 23442;
However, this provides no guarantees of correctness. For example, this model makes it possible for items of the same box to have different users, or titles. And, since this makes boxes a counter column family, it limits how you can evolve the schema in the future.
I think in PlayOrm's objects first, then show the column model below....
Box {
#NoSqlId
String id;
#NoSqlEmbedded
List<Item> items;
}
User {
#NoSqlId
TimeUUID uuid;
#OneToMany
List<Box> boxes;
}
The User then is a row like so
rowkey = uuid=<someuuid> boxes.fkToBox35 = null, boxes.fktoBox37=null, boxes.fkToBox38=null
Note, the form of the above is columname=value where some of the columnnames are composite and some are not.
The box is more interesting and say Item has fields name and idnumber, then box row would be
rowkey = id=myid, items.item23.name=playdo, items.item23.idnumber=5634, itesm.item56.name=pencil, items.item56.idnumber=7894
I am not sure what you meant though on get the top 20 boxes? top boxes meaning by the number of items in them?
Dean
You can use Query-Driven Methodology, for data modeling.You have the three broad access paths:
1) partition per query
2) partition+ per query (one or more partitions)
3) table or table+ per query
The most efficient option is the “partition per query”. This article can help you in this case, step-by-step. it's sample is exactly a one-to-many relation.
And according to this, you will have several tables with some similar columns. You can manage this, by Materialized View or batch-log(as alternative approach).

How to set an object to null

How to set an object to null.
Ex:
my object samp contains three fileds
samp.field1,samp.field2.sampfield3
If i set samp:= null;
im getting errors is there a way to set the object value to null.
An sql database does not know about objects, it deals with rows in table.
To remove a row use DELETE :
e.g. :
DELETE FROM samp WHERE id = 12345;
DELETE FROM samp WHERE field1 = 'Delete Me';
The first example is typical to remove individual rows uing their primary key (id in this case)
The second example will remove a group of rows which have a speciic value for a field.